Java & J2EE Page 3 - Primitive Data Types and Basic Language Rules for Java |
If we want to get crazy (and deciding to learn any programming language is a sure indicator that you do), Multidimensional Arrays offer you a way to get there. The following code demonstrates how to declare a two-dimensional array. Int YourTwoDimensionalArray [] [] = new int [8] [8]; What the above code does is give us an 8x8 array. Think of the way a chess board looks: a grid of 8 rows and 8 columns.
Each of the above boxes would hold two indexes; the left index is used for the row, the right index is used for column When you allocate memory for a multidimensional array, you only have to set memory for the left dimension. You can allocate the right dimension at a later time, as in the following code. Int YourTwoDimensionalArray [] [] = new [5] [0] YourTwoDimensionalArray[0] = new int [2] YourTwoDimensionalArray[0] = new int [7] YourTwoDimensionalArray[0] = new int [20] YourTwoDimensionalArray[0] = new int [25] YourTwoDimensionalArray[0] = new int [12] Note also that if you ever need to know the length of an array the following code will print it to your screen: System.out.println(YourArray.length);
blog comments powered by Disqus |
|
|
|
|
|
|
|