CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
Java allows you to easily create multidimensional arrays:
//: MultiDimArray.java
// Creating multidimensional arrays.
import java.util.*;
public class MultiDimArray
public static void main(String[] args) ,
,
};
for(int i = 0; i < a1.length; i++)
for(int j = 0; j < a1[i].length; j++)
prt('a1[' + i + '][' + j +
'] = ' + a1[i][j]);
// 3-D array with fixed length:
int[][][] a2 = new int[2][2][4];
for(int i = 0; i < a2.length; i++)
for(int j = 0; j < a2[i].length; j++)
for(int k = 0; k < a2[i][j].length;
k++)
prt('a2[' + i + '][' +
j + '][' + k +
'] = ' + a2[i][j][k]);
// 3-D array with varied-length vectors:
int[][][] a3 = new int[pRand(7)][][];
for(int i = 0; i < a3.length; i++)
for(int i = 0; i < a3.length; i++)
for(int j = 0; j < a3[i].length; j++)
for(int k = 0; k < a3[i][j].length;
k++)
prt('a3[' + i + '][' +
j + '][' + k +
'] = ' + a3[i][j][k]);
// Array of non-primitive objects:
Integer[][] a4 = ,
,
,
};
for(int i = 0; i < a4.length; i++)
for(int j = 0; j < a4[i].length; j++)
prt('a4[' + i + '][' + j +
'] = ' + a4[i][j]);
Integer[][] a5;
a5 = new Integer[3][];
for(int i = 0; i < a5.length; i++)
for(int i = 0; i < a5.length; i++)
for(int j = 0; j < a5[i].length; j++)
prt('a5[' + i + '][' + j +
'] = ' + a5[i][j]);
}
static void prt(String s)
The code used for printing uses length so that it doesn't depend on fixed array sizes.
The first example shows a multidimensional array of primitives. You delimit each vector in the array with curly braces:
int[][] a1 = ,
,
};
Each set of square brackets moves you into the next level of the array.
The second example shows a three-dimensional array allocated with new. Here, the whole array is allocated at once:
int[][][] a2 = new int[2][2][4];
But the third example shows that each vector in the arrays that make up the matrix can be of any length:
int[][][] a3 = new int[pRand(7)][][];
for(int i = 0; i < a3.length; i++)
The first new creates an array with a random-length first element and the rest undetermined. The second new inside the for loop fills out the elements but leaves the third index undetermined until you hit the third new.
You will see from the output that array values are automatically initialized to zero if you don't give them an explicit initialization value.
You can deal with arrays of non-primitive objects in a similar fashion, which is shown in the fourth example, demonstrating the ability to collect many new expressions with curly braces:
Integer[][] a4 = ,
,
,
};
The fifth example shows how an array of non-primitive objects can be built up piece by piece:
Integer[][] a5;
a5 = new Integer[3][];
for(int i = 0; i < a5.length; i++)
The i*j is just to put an interesting value into the Integer.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 1295
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved