Docsity

Multidimensional Arrays - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Multidimensional Arrays - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Multidimensional Arrays in Java

The Basic Definitions

• What is an Array?

  • An array is a fixed size sequential collection of elements of identical types.
  • They are created with either a new operator or array initializes
  • The storage space for arrays is allocated from the garbage-collected heap.
  • The element in an array are indexed by the integers 0 to n- 1, where n is the size of the array

Declaring an Array

• Using the one-dimensional array initializer

 { v 0 , v 1 , ….., v n-1}

The values for v 0 , v 1 , …..,vn-1 are the initial values of the elements in the

array. The size of the array is determined by the number of initial values provided in the array initializer

Int ia2 [ ] = {1, 2, 3};

// Creates an array of size 3; elements are initialized to 1, 2, and 3 respectively

What are Multidimensional Arrays

  • A multidimensional array is treated as an array of arrays. - Let a be a k-dimensional array; the elements of a

can be accessed using the following syntax:

a [ i 1 ] [ i 2 ]…[ ik ]

where n 1 , n2, ….., n k

Creating k-dimensional Array

Using the new operator

new Type [ n1] [n2]…[nk]

  • Size of each dimension is n1, n2, …,nk, respectively.
  • All the elements in the array are initialized to default initial values based on the type of the elements - Using k-dimensional array initializer

{ I 1 , I 2 , ...., I k}

  • where each I 1 , I 2 , ...., I k is a (k-1)- dimensional array initializer.

Examples…

  • Using the new operator, the following example is of creating and initializing a two-dimensional array.

double mat1[ ][ ] = new double[4][5];

This creates a 4 x 5 two-dimensional array. All elements are initialized to 0.0.

  • Using k-dimensional array initializer, the following example is of creating and initializing a two-dimensional array.

int mat2[ ] [ ] = {{1, 2, 3}, {4, 5, 6}};

This creates a 2 x 3 two-dimensional array. The elements are initialized as:

mat2[0][0]=1 mat2[1][0]= 4 mat2[0][1]=2 mat2[1][1]= 5 mat2[0][2]=3 mat2[1][2]= 6

Processing Multidimensional

Arrays

  • Multidimensional arrays are often processed using for statements. To process all the items in a two-dimensional array, a for statement needs to be nested inside another. int[ ][ ] A = new int[3][4]; The following loop stores 0 into each location in two dimensional array A : for (int row = 0; row < 3; row++) { for (int column = 0; column < 4; column++) { A [row][column] = 0; } }

An Example:

• Using a mathematical function in the Java

Math class and handling of two–dimensional

arrays, the following example calculates the

max – min value.

Keep in mind that…

  • Computer memory is linear –
  • In accessing a multidimensional array you are really accessing a one-dimensional array in memory.
  • You can simulate multidimensional arrays by packing them into a one-dimensional array.
  • The two manners of packing the one-dimensional array are row-major order, where the array is filled one row at a time; and column-major order, where columns are placed into the array.

Interesting Tidbits about Arrays in

Java

  • One of the main differences between the arrays in Java and those in C/C++ is that the Java arrays are ALWAYS bounds- checked at run time.
  • Array bound-checking will automatically detect a major source of faults in programs, that is exceeding the bounds of arrays while accessing or manipulating their elements.
  • There is no limit on the number of dimensions that an array type can have. However, arrays of dimension three or higher are fairly uncommon.