array1D[0] = 10.0;
And to use them you need to get the data from them, thus:
System.out.println(array1D[0]);
int array1D[] = new int[100];
for (int i = 0; i < array1D.length; i++) {
array1D[i] = 10;
System.out.println(array1D[i]);
}
array1D.length
, which is never reached (and quite right too - remember that numbering of positions starts at zero).
array1D[i] = 10 * i;
array1D[i] = // some code to read data
// using some pre-existing array: arrayA.
int[] arrayB = new int[arrayA.length + 1];
for (int i = 0; i < arrayA.length; i++) {
arrayB[i] = arrayA[i];
}
arrayA = null;
arrayB[arrayB.length - 1] = 10;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.println (i + " " + j);
}
}
Remember, that variables are destroyed as the processing leaves their scope, and re-made if the code runs again.
What do you think happens to j then, and where does the code go next?
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.println (i + " " + j);
}
}
Thus, each time the outer loop runs once, the inner loop runs to completion.
i | j | |
---|---|---|
0 | 0 | |
0 | 1 | |
0 | 2 | |
1 | 0 | |
1 | 1 | |
1 | 2 |
int array2D [][] = new int[2][3];
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[i].length; j++) {
array2D[i][j] = 10;
System.out.println (array2D[i][j]);
}
}
Note that i is in scope in the inner block, so we can use array2D[i].length
to cope with irregular arrays.
int array2D [][] = new int[2][3];
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[j].length; i++) {
array2D[i][j] = 10;
System.out.println (array2D[j][i]);
}
}
int array2D [][] = new int[2][3];
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[j].length; i++) {
array2D[i][j] = 10;
System.out.println (array2D[j][i]);
}
}
array2D[j].length
: Looping through to the wrong dimension length. This is very common if the lengths are hard-wired in, so avoid that.
int array2D [][] = new int[2][3];
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[j].length; i++) {
array2D[i][j] = 10;
System.out.println (array2D[j][i]);
}
}
i++
: Cutting and pasting your outer loop to make your inner loop, and forgetting to change part of the variable use; here, the inner increment should be to j.
int array2D [][] = new int[2][3];
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[j].length; i++) {
array2D[i][j] = 10;
System.out.println (array2D[j][i]);
}
}
System.out.println (array2D[j][i])
: Switching the indices the wrong way round. This should be array2D[i][j]
. With an non-square array, this will result in trying to read off one side of the array and the program will break. Worse, with a square array, your data will silently be transposed.
If you get confused, run through your algorithm by hand on paper, using a 2 by 3 non-square array.
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[i].length; j++) {
System.out.println(array2D[i][j]);
}
}
for (int i = 0; i < arrayA.length; i++) {
for (int j = 0; j < arrayA[i].length; j++) {
arrayB[i][j] = arrayA[i][j];
}
}
for (int i = 1; i < arrayA.length - 1; i++) {
for (int j = 1; j < arrayA[i].length - 1; j++) {
arrayB[i][j]= arrayA[i-1][j-1];
}
}
Wrap boundaries:
Suitable for modelling abstract landscapes |
|
Only process as many cells as you can:
Suitable for modelling non-abstract landscapes |
|
Only process cells that can have complete processing:
Suitable for image processing |