<< Chapter < Page Chapter >> Page >
  • Initially, the variable fib of type integer array is allocated. As part of the same statement, the array object is created with its seven fields having the values in the initializer; the reference to the object is returned and stored in the variable fib .
  • A reference to the array is passed as a parameter to the method first . The formal parameter a contains a reference to the same array pointed to by the actual parameter fib .
  • A new array b of the same type as the parameter a but half the length is declared and allocated.
  • Each iteration of the for-loop moves one element from the first half of a to the corresponding element in the array b .
  • The reference to array b is returned. Although array referenced by b was allocated within the method call, it still exists after returning.
  • There are no references to the original array so it is inaccessible. Jeliot does not visualize garbage collection so the arrayremains visualized in the Instance and Array Area until the end of the program.

Exercise Modify the program so that the original array remains accessible in a different field.

Two-dimensional arrays

Concept A matrix can be stored in a two-dimensional array. The syntax is int[][] with two indices, the first for rows and the second for columns. To access an element of the array, expressions for the twoindices must be givien.

Program: Array06.java

// Learning Object Array06 //     two-dimensional arrayspublic class Array06 {     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[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 sum = addElements(matrix);    } }

This program creates a 2 × 2 matrix and computes the sum of its elements.

  • 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 each row 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 the columns 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.
  • The reference to the array is passed as a parameter to the method addElements , which adds the values of all the elements.
  • The sum is returned from the method and assigned to the variable sum .

Exercise Modify the program perform the same computation on a 2 × 3 matrix and on a 3 × 2 matrix.

Arrays of 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. Therefore, byusing only one index a one-dimensional array is obtained.

Questions & Answers

what are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is biology
Inenevwo
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
what's cornea?
Majak Reply
what are cell
Achol
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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