Reputation:
If one had a set of 2d-arrays of different dimensions, say:
float[][] P = new float[2][3];
float[][] B = new float[2][2];
float[][] A = new float[32][2];
float[] E = new float[2];
currently holding no values.
Is it possible to add values to each index of the arrays by looping through the index values of the largest collective dimensions (in this case):
for (int i=0; i<32; i++){
for (int j=0; j<3; j++){
//doSomething to (P[i][j]) if P has this index
//doSomething to (B[i][j]) if B has this index
//doSomething to (A[i][j]) if A has this index
//doSomething to (E[i][j]) if E has this index
}
}
Otherwise, one must have four separate loops!
Thank you.
Upvotes: 0
Views: 40
Reputation: 6755
for (int i=0; i<32; i++){
for (int j=0; j<3; j++){
if(i < 2)
doSomething(P[i][j]);
if(i < 2 && j < 2)
doSomething(B[i][j]);
if(j < 2)
doSomething(A[i][j]);
if(i == 0 && j < 2)
doSomething(E[i][j]);
}
}
You need the i == 0
part for E
because you only want to modify everything once, not 32 times. Note that only B
needs two conditions because the other conditions are enforced by the for
loop.
Upvotes: 1