【发布时间】:2010-11-26 23:46:43
【问题描述】:
我的 java 程序中有以下二维数组。
Integer[][] arrayOfSets = {
{1,2,5,6,9,10},
{9,10,11,12},
{1,2,3,4},
{3,5,6,7,8},
{9,10,11,12},
{4,8},
};
谁能告诉我如何使程序动态的代码,程序要求用户输入行号和列号,即二维数组应该包含多少个数组,然后询问用户有多大每个数组以及每个数组应包含的数字是多少? 提前非常感谢。
这是我迄今为止尝试过的:
System.out.println("HOw many sets arrays would you like");
int numArrays=sc.nextInt();
Integer [][] arrayName = new Integer[numArrays][];
for(int i=0;i<arrayName.length;i++){
System.out.println("enter the size of the array number"+i);
int sizeArray=sc.nextInt();
for(int k=0;k<sizeArray;k++){
System.out.println("enter element");
int e=sc.nextInt();
arrayName[i][k]=e;
}
}
这看起来对吗?感谢您提前提供的所有帮助。
好的,我再次修改了我的鳕鱼。以下是我现在拥有的:
System.out.print("你想要多少个数组?"); int numSets=sc.nextInt(); 整数[][] 结果 = 新整数[numSets][];
for (int k=0; k< numSets; k++){
System.out.println("Enter the size of the array");
int setSize = sc.nextInt();
Integer[] row = new Integer[setSize];
for (int m=0; m< setSize; m++){
System.out.println("enter element: ");
row[m] = sc.nextInt();
}
result[k] = row;
}
【问题讨论】:
-
请查看我的代码,如果正确,请告诉我。谢谢。
-
观察:Java 不支持真正的多维数组;相反,它支持一维数组,其中每个元素本身可以是另一个一维数组。区别在于分配内存的方式,其中
a[i][j]与a[i,j]不同。 -
Does this look right?我不知道,你遇到了什么错误?