[英]Iterating through all slots that are not null, 2d array?
I must work with a 2d array. 我必须使用二维数组。 The maximum length of the row slots in the array is 100. More often than not, anywhere from 5-20 of these array slots will be filled and not more, however, I must build my code to a max of 100 rows. 数组中行插槽的最大长度为100。通常,这些数组插槽中的5-20个会被填充,并且不会更多,但是,我必须将代码构建为最多100行。 My question is, is there a way to iterate through only the array slots that have been set, stopping before the last unset, null slots? 我的问题是,是否有一种方法可以仅循环访问已设置的数组插槽,并在最后一个未设置的空插槽之前停止?
//initialize array
String[][] variables = new String[numVariables][100];
//System.out.printf("%s \n", numVariables);
for(int i=0; i < numVariables; i++){
//reads in variable line
String variableLine = fin.nextLine();
//turn variable line into array
varArray = variableLine.split(" ");
numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));
for(int j=0; j < numRules+1; j++){
variables[i][j] = varArray[j+1];
System.out.printf("%s ", variables[i][j]);
}
System.out.println("\n");
}
//**LATER IN MY CODE ****//
//ITERATE THROUGH 'variables' array and PRINT OUT ONLY VALUES THAT ARE SET
If you populate the array in order from 0 to 100. If the first 51 elements are populated with the string then you could use: 如果按从0到100的顺序填充数组。如果使用字符串填充前51个元素,则可以使用:
for(int i=0; i < numVariables; i++){
for(int j=0; j < numRules+1; j++){
if (variables[i][j] == null)
break;
System.out.printf("%s ", variables[i][j]);
}
}
Why do you even store the nulls if you don't need them? 如果不需要空值,为什么还要存储空值? A String[][]
is an array of arrays of String - those inner arrays need not have the same length. String[][]
是String数组的数组-那些内部数组不必具有相同的长度。 You could therefore create each inner array with the number of elements it needs: 因此,您可以使用所需的元素数来创建每个内部数组:
//initialize array
String[][] variables = new String[numVariables][];
//System.out.printf("%s \n", numVariables);
for(int i=0; i < numVariables; i++){
//reads in variable line
String variableLine = fin.nextLine();
//turn variable line into array
varArray = variableLine.split(" ");
numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));
variables[i] = Arrays.copyOfRange(varArray, 1, numRules - 1);
}
and then iterate: 然后迭代:
for (String[] var : variables) {
for (String s : var) {
System.out.print(s);
}
System.out.println();
}
You can either use an array of List<String>
or you can also keep track of the length of all your rows. 您可以使用List<String>
的数组,也可以跟踪所有行的长度。
int [] rowLength = new int[numVariables]
for(int i=0; i < numVariables; i++){
/* ... */
numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));
// store the number of slots in variable[i]
rowLength[i] = numRules+1;
/* ... */
}
Then you iterate only from zero to the length of your row ( rowLength[i]
). 然后,您仅从零迭代到行的长度( rowLength[i]
)。
A third approach (and the one I would prefer ) is to not specify the length of the row slots in your array : 第三种方法( 也是我更喜欢的一种方法)是不指定数组中行槽的长度:
String [][] variables = new String[numVariables][];
Then after having calculated numRules: 然后在计算了numRules之后:
variables[i] = new String[numRules+1];
Given the situation you have described you'd probably be better off using another data structure, but seeing as you cannot you could keep a record of what values have been changed. 考虑到您描述的情况,使用其他数据结构可能会更好,但是看到您无法保存更改值的记录。
So you could keep an array of the length of each row and end each search there for the row. 因此,您可以保留每行长度的数组,并在该行结束每次搜索。
String[][] variables = new String[numVariables][100];
int rowLength[] = new int[numVariables];
//...
// get and record the rowLength
//...
for(int x=0; x < numVariables; x ++) {
for(int y=0; y < rowLength[x]; y ++) {
// do your stuff
}
}
}
Or you could use a map or ArrayList to keep track of each of the positions that contain numbers. 或者,您可以使用地图或ArrayList来跟踪每个包含数字的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.