<< Chapter < Page | Chapter >> Page > |
There are many ways to store data. So far, we have covered linear recursive structures, lists, and binary recursive structures, trees. Let's consider another way of storing data, as a contiguous, numbered (indexed) set of data storage elements:
anArray =
itemA | itemB | itemC | itemD | itemE | itemF | itemG | itemH | itemI | itemJ |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
This " array " of elements allows us to access any individual element using a numbered index value.
new
), andObject
or primitivesarrayOfInts[i]
refers to the
i+1 st element in the
arrayOfInts
array. In computer-ese, an array is said to be a "random access" container, because you can directly (and I suppose, randomly) access any element in the array.arrayOfInts.length
.More information on arrays can be found in the Java Resources web site page on arrays
int[]
is the type corresponding to a one-dimensional array of integers.double[][]matrixOfDoubles;
declares a variable whose type is a two-dimensional array of double-precision floating-point numbers.double rowvector[], colvector[], matrix[][];
This declaration is equivalent to
double[] rowvector, colvector, matrix[];
or
double[] rowvector, colvector;double[][]matrix;
Please use the latter!new
. For example,
String[] arrayOfStrings = new String[10];
declares a variable whose type is an array of strings, and initializes it to hold a reference to an array object with room for ten references to strings.int[] arrayOf1To5 = { 1, 2, 3, 4, 5 };String[] arrayOfStrings = { "array","of",
"String" };Widget[] arrayOfWidgets = { new Widget(), new Widget() };
int[][]arrayOfArrayOfInt = {{ 1, 2 }, { 3, 4 }};
int[] arrayOf1To5 = { 1, 2, 3, 4, 5 };System.out.println(arrayOf1To5.length);
would print ``5''.Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?