<< Chapter < Page Chapter >> Page >

Program: Array07.java

// Learning Object Array07 //    arrays of arrayspublic class Array07 {     public static void main(/*String[] args*/) {         int[][] matrix = new int[2][2];        for (int i = 0; i < matrix.length; i++)             for (int j = 0; j < matrix[i].length; j++)                matrix[i][j] = i*matrix.length + j;         int[] vector = matrix[1];    } }

This program creates a 2 × 2 matrix and then assigns the second row to a variable of type one-dimensional array.

  • A two-dimensional array is allocated, the reference to it is assigned to the variable matrix . The variable matrix contains one reference for each row and the rows are allocated as separate objects. Note that Jeliot displays rows from top to bottom as it does for all objects!
  • The elements of the array are initialized to (0,1,2,3) in a nested for-loop. The outside loop iterates over the rows and the inner loop iterates over thecolumns within an array.
  • matrix.length is used to get the number of rows and matrix[i].length to get the number of columns in row i , which is the same for all rows in this program.
  • A variable vector of type one-dimensional array is declared and initialized with the second row of the matrix, matrix[1] .

Exercise Write a program to rotate the rows of the array matrix . That is, row 0 becomes row 1 and row 1 becomes row 0. Now do this for an array of size 3 × 3 : row 0 becomes row 1, row 1 becomes row 2 and row 2 becomes row 0.

Ragged arrays

Concept A two-dimensional array is really an array of arrays; that is, each element of the array contains a reference to another array. However, the two-dimensional array need not be a square matrix, and each row can have a different number of elements. By using only one index a one-dimensional array is obtained and these arrays need not all be of the same size.

Program: Array08.java

// Learning Object Array08 //    ragged arrayspublic class Array08 {     static int addElements(int[][] a) {        int sum = 0;         for (int i = 0; i < a.length; i++)             for (int j = 0; j < a[i].length; j++)                sum = sum + a[i][j];         return sum;    }      public static void main(/*String[] args*/) {        int[][] matrix = new int[3][];         int[] row0 = {0, 1, 2};         int[] row1 = {3, 4};         int[] row2 = {5};         matrix[0] = row0;         matrix[1] = row1;         matrix[2] = row2;         int sum = addElements(matrix);    } }

Here we create the upper-left triangle of a 3 × 3 matrix: row 0 of length 3, row 1 of length 2 and row 2 of length 1. Then we add the elements of the “ragged” array.

  • The variable matrix is allocated, but since the size of the rows is not given, it is allocated as a one-dimensional array whose elements are references to one-dimensional arrays of integers. The default value for the elements is null .
  • Three rows of different size are allocated with initializers and assigned to the elements of the array matrix .
  • A reference to matrix is passed to the method addElements which adds the elements of the array and returns the value.
  • matrix.length is used to get the number of rows and matrix[i].length to get the number of columns in row i ; these are different for each row.

Exercise Simplify the allocation of the array matrix . First, show how the variables row can be eliminated. Then find out how to write an initializer for a two-dimensional array so that the array can be initialized in one declaration. (Note: initializers for two-dimensional arrays are not supported in Jeliot.)

Arrays of objects

Concept Arrays can contain references to arbitrary objects. There is no difference between these arrays and arrays whose values are of primitive type, except that an individual element can be of any type.

Program: Array09.java

// Learning Object Array09 //    arrays of objectsclass Access {     String name;    int level;     Access(String u, int l) {        name = u; level = l;     }}  public class Array09 {     static void swap(Access[] a, int i, int j) {         Access temp = a[i];         a[i] = a[j];        a[j] = temp;    }      public static void main(/*String[] args*/) {         Access[] accesses = new Access[2];        accesses[0] = new Access("Bob", 3);        accesses[1] = new Access("Alice", 4);        swap(accesses, 0, 1);     }}

Objects of class Access contain name of a bank customer and the access level permitted for that customer. This program creates two objects, assigns the their references to elements of the Access and then swaps the elements of the array.

  • The array accessess of type Access[] is allocated and contains null references.
  • An object of type Access are allocated and initialized by its constructor; a reference to the object is stored in the first element of the array accessess .
  • Similarly, another object is created and stored in the second element.
  • The array accessess is passed to the method swap along with the indices 0 and 1.
  • The two elements of the array are swapped. Note that after executing a[i] = a[j] , both elements of the array point to the second object ( Alice,4 ), while a reference to the first object ( Bob,3 ) is saved in the variable temp .

Exercise Modify the program so that the initialization of the array accessess is done in one statement instead of three.

Exercise Explain what happens if the method swap is replaced by:

static void swap(Access a, Access b) {   Access temp = a;  a = b;   b = temp;}

and the call by swap(accesses[0], accesses[1]);

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask