<< Chapter < Page Chapter >> Page >

Types of data that you can store in an array object

Array elements can contain any Java data type including primitive values and references to ordinary objects or references to other array objects.

Constructing multi-dimensional arrays

All array objects contains a one-dimensional array structure. You can create multi-dimensional arrays by causing the elements in one array object to containreferences to other array objects. In effect, you can create a tree structure of array objects that behaves like a multi-dimensional array.

Odd-shaped multi-dimensional arrays

The program array01 shown in Listing 1 illustrates an interesting aspect of Java arrays. Java can produce multi-dimensional arrays that can be thought of as an array of arrays. However, the secondary arrays neednot all be of the same size.

In the program shown in Listing 1 , a two-dimensional array of integers is declared and instantiated with the primary size (size of the first dimension) being three. The sizes of the secondary dimensions (sizes of each of the sub-arrays) is 2, 3, and 4 respectively.

Can declare the size of secondary dimension later

When declaring a "two-dimensional" array, it is not necessary to declare the size of the secondary dimension when the primary array isinstantiated. Declaration of the size of each sub-array can be deferred until later asillustrated in this program.

Accessing an array out-of-bounds

This program also illustrates the result of attempting to access an element that is out-of-bounds. Java protects you from suchprogramming errors.

ArrayIndexOutOfBoundsException

An exception occurs if you attempt to access out-of-bounds, as shown in the program in in Listing 1 .

In this case, the exception was simply allowed to cause the program to terminate. The exception could have been caught and processed by an exceptionhandler, a concept that will be explored in a future module.

The program named array01

The entire program is shown in Listing 1 . The output from the program is shown in the comments at thetop of the listing.

Listing 1 . The program named array01.
/*File array01.java Copyright 1997, R.G.Baldwin Illustrates creation and manipulation of two-dimensionalarray with the sub arrays being of different lengths. Also illustrates detection of exception when an attempt ismade to store a value out of the array bounds. This program produces the following output:00 0120246 Attempt to access array out of boundsjava.lang.ArrayIndexOutOfBoundsException: at array01.main(array01.java: 47)**********************************************************/ class array01 { //define the controlling classpublic static void main(String[] args){ //main method//Declare a two-dimensional array with a size of 3 on // the primary dimension but with different sizes on// the secondary dimension. //Secondary size not specified initiallyint[][]myArray = new int[3][]; myArray[0]= new int[2];//secondary size is 2myArray[1] = new int[3];//secondary size is 3 myArray[2]= new int[4];//secondary size is 4//Fill the array with data for(int i = 0; i<3; i++){ for(int j = 0; j<myArray[i].length; j++){myArray[i][j]= i * j; }//end inner loop}//end outer loop //Display data in the arrayfor(int i = 0; i<3; i++){ for(int j = 0; j<myArray[i].length; j++){System.out.print(myArray[i][j]); }//end inner loopSystem.out.println(); }//end outer loop//Attempt to access an out-of-bounds array element System.out.println("Attempt to access array out of bounds"); myArray[4][0] = 7;//The above statement produces an ArrayIndexOutOfBounds // exception.}//end main }//End array01 class.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask