I am attempting to solve a semi-difficult problem in which I am attempting to create an array and return a 3 dimensional array based on the parameter which happens to be a 2 dimensional int array. The array I'm attempting to return is a String array of 3 dimensions. So here is the code:
我试图解决一个半难的问题,我试图创建一个数组并返回一个基于参数的三维数组,该参数恰好是一个二维int数组。我试图返回的数组是一个3维的String数组。所以这是代码:
public class Displaydata {
static String[][][] makeArray(int[][] dimensions) {
String myArray[][][];
for (int i = 0; i < dimensions.length; i++) {
for (int j = 0; j < dimensions[i].length; j++) {
myArray[][][] = new String[i][j][]; //getting error here.
}
}
return myArray;
}
static void printArray(String[][][] a) {
for (int i = 0; i < a.length; i++) {
System.out.println("\nrow_" + i);
for (int j = 0; j < a[i].length; j++) {
System.out.print( "\t");
for (int k = 0; k < a[i][j].length; k++)
System.out.print(a[i][j][k] + " ");
System.out.println();
}
}
}
public static void main(String[] args) {
int [][] dim = new int[5][];
dim[0] = new int[2];
dim[1] = new int[4];
dim[2] = new int[1];
dim[3] = new int[7];
dim[4] = new int[13];
dim[0][0] = 4;
dim[0][1] = 8;
dim[1][0] = 5;
dim[1][1] = 6;
dim[1][2] = 2;
dim[1][3] = 7;
dim[2][0] = 11;
for (int i = 0; i < dim[3].length;i++)
dim[3][i] = 2*i+1;
for (int i = 0; i < dim[4].length;i++)
dim[4][i] = 26- 2*i;
String[][][] threeDee = makeArray(dim);
printArray(threeDee);
}
}
As you can see from the source code, I'm getting an error when I try to create an instance of my 3-dimensional array which I'm attempting to return. I'm supposed to create a three dimensional array with the number of top-level rows determined by the length of dimensions and, for each top-level row i, the number of second-level rows is determined by the length of dimensions[i]. The number of columns in second-level row j of top-level row i is determined by the value of dimensions[i][j]. The value of each array element is the concatenation of its top-level row index with its second-level row index with its column index, where indices are represented by letters : ‘A’ for 0, ‘B’ for 1 etc. (Of course, this will only be true if the indices don’t exceed 25.) I don't necessarily know where I'm going wrong. Thanks!
从源代码中可以看出,当我尝试创建一个我试图返回的三维数组的实例时,我遇到了错误。我应该创建一个三维数组,顶层行的数量由维度的长度决定,对于每个*行i,二级行的数量由维度的长度决定[i ]。顶层行i的第二级行j中的列数由dimension [i] [j]的值确定。每个数组元素的值是其*行索引与其第二级行索引及其列索引的串联,其中索引由字母表示:'A'代表0,'B'代表1等。当然,如果指数不超过25,这只会是真的。)我不一定知道我哪里出错了。谢谢!
4 个解决方案
#1
1
You should not be initializing the array on every iteration of the loop. Initialize it once outside the loop and then populate it inside the loop.
您不应该在循环的每次迭代中初始化数组。在循环外部初始化它,然后在循环内填充它。
static String[][][] makeArray(int[][] dimensions) {
String[][][] myArray = new String[25][25][1];
for (int i = 0; i < dimensions.length; i++) {
for (int j = 0; j < dimensions[i].length; j++) {
myArray[i][j][0] = i + "," + j;
}
}
return myArray;
}
I just plugged in values for the size of the first two dimensions, you will need to calculate them based on what you put in there. The 'i' value will always be dimensions.length but the 'j' value will be the largest value returned from dimensions[0].length -> dimensions[n-1].length where 'n' is the number of elements in the second dimension.
我只是为前两个维度的大小插入了值,您需要根据放在那里的内容来计算它们。 'i'值将始终为dimensions.length,但'j'值将是dimension [0]返回的最大值.length - > dimensions [n-1] .length其中'n'是元素的数量第二个维度。
Also you will need to set up a way to convert the numbers in 'i' and 'j' to letters, maybe use a Map.
此外,您还需要设置一种方法,将'i'和'j'中的数字转换为字母,也可以使用Map。
#2
1
I guess you should initialize the array as myArray = new String[i][j][]; //getting error here.
我猜你应该将数组初始化为myArray = new String [i] [j] []; //在这里得到错误
#3
1
I think
myArray[][][] = new String[i][j][]; //getting error here.
should be:
myArray[i][j] = new String[5]; // I have no idea how big you want to go.
And then you can fill in each element of you inner-most array like such:
然后你可以填写你最内部数组的每个元素,如:
myArray[i][j][0] = "first item";
myArray[i][j][1] = "second string";
...
#4
0
I think you should just change that line to:
我想你应该把这一行改为:
myArray = new String[i][j][]; //look ma! no compiler error
Also, you would need to initialize myArray
to something sensible (perhaps null
?)
此外,您需要将myArray初始化为合理的(可能为null?)
#1
1
You should not be initializing the array on every iteration of the loop. Initialize it once outside the loop and then populate it inside the loop.
您不应该在循环的每次迭代中初始化数组。在循环外部初始化它,然后在循环内填充它。
static String[][][] makeArray(int[][] dimensions) {
String[][][] myArray = new String[25][25][1];
for (int i = 0; i < dimensions.length; i++) {
for (int j = 0; j < dimensions[i].length; j++) {
myArray[i][j][0] = i + "," + j;
}
}
return myArray;
}
I just plugged in values for the size of the first two dimensions, you will need to calculate them based on what you put in there. The 'i' value will always be dimensions.length but the 'j' value will be the largest value returned from dimensions[0].length -> dimensions[n-1].length where 'n' is the number of elements in the second dimension.
我只是为前两个维度的大小插入了值,您需要根据放在那里的内容来计算它们。 'i'值将始终为dimensions.length,但'j'值将是dimension [0]返回的最大值.length - > dimensions [n-1] .length其中'n'是元素的数量第二个维度。
Also you will need to set up a way to convert the numbers in 'i' and 'j' to letters, maybe use a Map.
此外,您还需要设置一种方法,将'i'和'j'中的数字转换为字母,也可以使用Map。
#2
1
I guess you should initialize the array as myArray = new String[i][j][]; //getting error here.
我猜你应该将数组初始化为myArray = new String [i] [j] []; //在这里得到错误
#3
1
I think
myArray[][][] = new String[i][j][]; //getting error here.
should be:
myArray[i][j] = new String[5]; // I have no idea how big you want to go.
And then you can fill in each element of you inner-most array like such:
然后你可以填写你最内部数组的每个元素,如:
myArray[i][j][0] = "first item";
myArray[i][j][1] = "second string";
...
#4
0
I think you should just change that line to:
我想你应该把这一行改为:
myArray = new String[i][j][]; //look ma! no compiler error
Also, you would need to initialize myArray
to something sensible (perhaps null
?)
此外,您需要将myArray初始化为合理的(可能为null?)