Object array[][] = new Object[3][3];
array[0] = new Object[3];
array[0][0] = new Object();
array[0][1] = new Object();
array[0][2] = new Object();
这个就是实现了只给第一行赋值,而另两行都是空的,以后随时想赋值都可以按照上面的代码继续。当然你也可以写个循环比如
Object array[][] = new Object[3][3];
array[0] = new Object[3];
for(int i = 0; iarray[0][i] = new Object();
可以动态初始化第一行,例如:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[][] arr = new int[3][3]; // 定义2维数组
int i, j;
// 动态初始化
arr[0][0] = 0;
arr[0][1] = 1;
arr[0][2] = 2;
Scanner input = new Scanner(System.in);
for (i = 1; i < 3; i++){
for (j = 0; j < 3; j++){
arr[i][j] = Integer.parseInt(input.next()); //输入整数
}
}
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
System.out.print(arr[i][j]);
System.out.print('\t');
}
System.out.println();
}
input.close();
}
}