
Java Arrays
Single Dimensional Arrays
Java arrays, like arrays in most other languages, consist of like types of items in a consecutive memory space. In order to create such a space the compiler must know how much memory to allocate and will only allocate exactly that much.
This sizing issue is why line 3 in the code below declares that the array someInts will be exactly the size of four ints since the size variable is set to 4. Values are then set dynamically, lines 4 – 7, at run-time (when the application is actually executing).
It is also possible to create, size, and insert array values at compile-time instead of run-time. Line 10 shows how to do this. Here, the otherInts array is sized to hold exactly 8 int primatives.
Whether the array is created at compile-time or run-time, iterating over and using the values is done exactly the same way. Lines 14 – 17 show how this is done.
Each element in the array is retrieved using standard Java array notation, the [] characters, where the number in-between the brackets is the index into the array for a specific array element.
Multi-Dimensional Arrays
Multi-dimensional arrays are created in much the same way as single dimensional, simple arrays. Line 28 shows how to create a two dimensional array at compile-time and Lines 29 – 42 show how to retrieve the data and use it.
Notice that since there are two dimensions to the array two for loops are required to retrieve the elements. The outer loop iterates over the rows of the array, the first dimension, and the inner loop iterates over the elements within each row. Also notice that the rows do not have to be the same length.
Sorting Arrays
There is a Java class called Arrays that has several static helper-functions created by Sun to make your life easier. One of these is the sort method. If you pass a single dimensional array of Java objects of known type, such as String, or primitives, int, double, byte, char, etc., to this method it sorts them within the existing array. Line 19 shows how this is done.
If you want custom types of objects, maybe your application has an array of Employee objects, you will have to create what is called a ‘comparator’. A comparitor is passed to the sort method as the second parameter and provides it with a way to compare the custom objects. This tutorial doesn’t cover this functionality but there are several examples on the web.
1 //create and size an array at run-time and then add values
2 int size = 4;
3 int[] someInts = new int[size];
4 someInts[0] = 144;
5 someInts[1] = 10;
6 someInts[2] = 65;
7 someInts[3] = -20;
8 System.out.println("count of some ints: "+someInts.length);
9 //create, size, and add values to an array statically
10 int[] otherInts = {10,55,4,5,6,7,8,9};
11 System.out.println("count of run-time ints: "+otherInts.length);
12 //access each value in an array and print out it's value
13 System.out.println("compile-time values");
14 for(int i = 0; i < otherInts.length; i++){
15 int aValue = otherInts[i];
16 System.out.println("\tvalue: "+aValue);
17 }
18 //sort an array of ints using the default sorting order
19 Arrays.sort(otherInts);
20 //access each value in an array and print out it's value
21 System.out.println("sorted values");
22 for(int i = 0; i < otherInts.length; i++){
23 int aValue = otherInts[i];
24 System.out.println("\tvalue: "+aValue);
25 }
26 System.out.println("static two dimentional Array");
27 //create, size, and add values to a two-dimentional array statically
28 int[][] multDimComptime = {{1,0},{0,5},{3,-2},{-1,0,500}};
29 //access each value and print out it's value
30 for(int i = 0; i < multDimComptime.length; i++){
31 //tab the printed values over
32 System.out.print("\t");
33 for(int j = 0; j < multDimComptime[i].length; j++){
34 System.out.print(multDimStatic[i][j]);
35 //add a comma between values but not at the end
36 if(j < multDimComptime[i].length -1){
37 System.out.print(", ");
38 }
39 }
40 //print a new line at the end of each row
41 System.out.print("\n");
42 }
43
44 //at run-time create and size a two dimentional array
45 int rows = 4;
46 int columns = 4;
47 int[][] moreMultDim = new int[rows][columns];
48 //dynamically add values to the array
49 moreMultDim[0][0] = 65;
50 moreMultDim[0][1] = 33;
51 moreMultDim[0][2] = -55;
52 moreMultDim[0][3] = 77;
53 moreMultDim[3][3] = 100;
54 System.out.println("dynamic two dimentional array");
55 for(int i = 0; i < moreMultDim.length; i++){
56 //tab the printed values over
57 System.out.print("\t");
58 for(int j = 0; j < moreMultDim[0].length; j++){
59 System.out.print(moreMultDim[i][j]);
60 //add a comma between values but not at the end
61 if(j < moreMultDim[0].length -1){
62 System.out.print(", ");
63 }
64 }
65 //print a new line at the end of each row
66 System.out.print("\n");
67 }