Arrays in Java
Arrays in Java
Arrays
Please note that this topic is not discussed in the lecture. Please cover it by yourself. Any queries are warmly welcomed.
Arrays in Java
Java does have a true array type with protective features to prevent your program from writing outside the memory bounds of the array. Arrays are treated as true objects in Java.
Declaring an array
In Java, you must declare an array before you can use it. In declaring the array, you must provide two important pieces of information:
-1-
Handout 32 Programming - 2
These two statements simultaneously declare the name of the array and cause memory to be allocated to contain the array. However there is another way of doing that. The initialization list is used for allocating memory as well as assigning values to elements of the array
int[] intArray = {3, 4, 2, 10, 7}; char[] charArray[] = {w, d, e};
In the above case, the java will determine the size of the array from the number of values provided in the initialization list. So, size of intArray would be 5 and charArray would be 3.
-2-
Handout 32 Programming - 2
//assigning 6 to array 3rd element myArray[2] = 6; //retrieving value of array 3rd element myVar = myArray[2];
All array objects in Java have a length property that can be accessed to determine the number of elements stored in the array. Remember: length is an property not a function, so no set of parentheses [ () ] are add.
-3-
Handout 32 Programming - 2
ArrayIndexOutOfBoundsException
In Java, an exception occurs if you attempt to access out-of-bounds, as shown in the given below sample program. In this case, the exception was simply allowed to cause the program to terminate. The exception could have been caught and processed by an exception handler, a concept that will be explored in depth later. To know more about Exceptions, see handout on it.
-4-
Handout 32 Programming - 2
//Display data in the array for(int i = 0; i < 3; i++){ for(int j = 0; j < myArray[i].length; j++){ System.out.print(myArray[i][j]); }//end inner loop System.out.println(); // moving cursor to new line }//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 ArrayTest class.
-5-
Handout 32 Programming - 2
Output
File ArrayTest.java Illustrates creation and manipulation of two-dimensional array with the sub arrays being of different lengths. Also illustrates detection of exception when an attempt Is made to store a value out of the array bounds. This program produces the following output:
00 012 0246 Attempt to access array out of bounds java.lang.ArrayIndexOutOfBoundsException: at array01.main(array01.java: 47)
//creating two arrays using initialization lists int int firstArray[] = {10, 20, 30}; secondArray[] = {40, 50, 60}; 0F32
10 20 30
firstArray
0F32
secondArray
0 9E21
40
1
50
2
60
9E21
-6-
firstArray
0F32
10 20 30
0F32
secondArray
0 9E21
40
1
50
2
60
0F32
// as you have clearly seen, secondArray is now also Pointing to the firstArray elements
secondArray = firstArray;
System.out.println( "firstArray contents" ); for(int i = 0; i < firstArray.lenght; i++) { System.out.print(firstArray[i] + " " ); } //moving cursor to new line System.out.println(); System.out.println( "secondArray contents" ); for(int i = 0; i < secondArray.lenght; i++)
-7-
Handout 32 Programming - 2 System.out.print(secondArray[i] + " " ); //moving cursor to new line System.out.println();
firstArray[1] = 10;
System.out.println( "firstArray contents" ); for(int i = 0; i < firstArray.lenght; i++) { System.out.print(firstArray[i] + " " ); } //moving cursor to new line System.out.println(); System.out.println( "secondArray contents" ); for(int i = 0; i < secondArray.lenght; i++) System.out.print(secondArray[i] + " " ); }//end main }//End ArrayTest class.
Output
File ArrayTest2.java Illustrates that when you assign one array to another array, you end up with two references to the same array. The output from running this program is:
firstArray contents 0 1 2 secondArray contents 0 1 2 Change a value in firstArray and display both again firstArray contents 0 10 2 secondArray contents 0 10 2