lecture-31 - Array Basics In Java we are not limited to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Array Basics In Java, we are not limited to fixed-size array declaration. The following code prompts the user for the size of an array and declares an array of designated size: int size; int number; size= Integer.parseInt( JOptionPane.showInputDialog(null, “Size of an array:”)); number = new int[size]; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 12 Array Basics Any valid integer arithmetic expression is allowed for specifying the size of an array: size = Integer.parseInt( JOptionPane.showInputDialog(null,””)); number = new int[size*size + 2* size + 5]; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 13 Arrays of Objects Arrays are not limited to primitive data types. We will use the Person class to illustrate this concept. An array of objects is declared and created just as an array of primitive data types is. Person person; person = new Person[20]; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 14 An array of Person objects after the array is created. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 15 Arrays of Objects Array elements are initially null. Since each array element is an object, each must also be created. person[0] = new Person( ); Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 16 The person array with one Person object added to it. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 17 Arrays of Objects To assign data values to this object, we can execute person[0].setName(“Ms. Latte”); person[0].setAge (20); person[0].setGender (‘F’); The syntax here is the one used to call an object’s method; we are using an indexed expression to refer to the object instead of a simple variable. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 18 An array of Person objects with two Person variables. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 19 Arrays of Objects Deleting objects from an array requires us to search for the Person object to be removed. When the object is located, there are two ways to delete the object. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 20 Arrays of Objects The first approach is to set the array element to null. Because each array element is a reference to an object, setting the element to null accomplishes this task easily. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 21 Approach 1 deletion: Setting a reference to null. The array length is 4. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 22 Arrays of Objects Any index position may be set to null, but this approach creates “holes” in the array. The second approach to deleting an object will order the elements so the real references occur at the beginning and the null references at the end. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 23 Packing the elements so the real references occur at the beginning and the null references occur at the end. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 24 Arrays of Objects We must fill the holes. There are two possible approaches: If an object at position J is removed (i.e., set to null), then elements from position J+1 up to the last non-null reference are shifted one position lower. Finally, the last non-null reference is set to null. Replace the removed element by the last element in the array. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 25 Approach 2 deletion: Replace the removed element with the last element in the array. The array length is 4. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 26 Arrays of Objects Note that assigning null to an array element will not erase the object. This operation does initiate a chain reaction that will eventually erase the object from memory. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 27 Passing Arrays to Methods Arrays and objects are reference data types, so the rules for passing an object to a method and returning an object from a method apply to arrays. Consider an example method that returns the index of the smallest element in an array of real numbers: Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 28 Passing Arrays to Methods public int searchMinimum(double number) { int indexOfMinimum = 0; for (int i = 1; i < number.length; i++){ if (number[i] < number[indexOfMinimum]) { //found a smaller element indexOfMinimum = i; } } return indexOfMinimum; } To call this method (from a method of the same class), we write Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 29 Passing Arrays to Methods double arrayOne; //create and assign values to arrayOne ... //get the index of the smallest element of arrayOne int minOne = searchMinimum(arrayOne); //output the result System.out.print(“Minimum value in Array One is ”); System.out.print(arrayOne[minOne] + “at position ” + minOne); ... Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 30 Passing Arrays to Methods Remember that when an array is passed to a method, only its reference is passed. A copy of the array is not created in the method. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 31 Passing Arrays to Methods Next we will consider an example in which we return an array from a method. This method inputs double values and returns the values as an array of double. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 32 Passing Arrays to Methods public double readDoubles() { double number; int N = Integer.ParseInt( JOptionPane.showInputDialog(null, input values?”)); “How many number = new double[N]; for (int i = 0; i<N; i++){ number[i] = Double.parseDouble( JOptionPane.showInputDialog(null, “Number ” + i)); } return number; } Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 33 Passing Arrays to Methods The readDoubles method is called: double //assign arrayOne arrayOne; values to arrayOne = readDoubles(); Because a new array is created by the method, we do not have to create an array from the calling side. Doing so will not cause an error, but it is a wasteful operation. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 34 Using Command-Line Arguments We discuss about the args parameter to main( ) that has been in every program shown so far. Java programs accept what are called command-line arguments A command-line argument is the information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program – they are stored as strings in the String array passed to main( ). Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 35 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 36 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 37 Two-Dimensional Arrays In Java, data may be organized in a twodimensional array. A table is an example of a two-dimensional array. In a two-dimensional array, two indices (in a table, one for the row and one for the column) are used to refer to the array element. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 38 Two-Dimensional Arrays To declare our example array, we state: double payScaleTable; or double payScaleTable; and create the array as payScaleTable = new double[4][5]; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 39 Two-Dimensional Arrays To refer to an element at the second column (column 1) of the third row (row 2), we say payScaleTable[2][1] Nested-for loops are useful for manipulating twodimensional arrays. In Java, the two-dimensional array concept is implemented by using an array of arrays. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 40 Two-Dimensional Arrays The sample array creation payScaleTable = new double[4][5]; is a shorthand for payScaleTable = new double [4][ ]; payScaleTable[0] payScaleTable[1] payScaleTable[2] payScaleTable[3] = = = = new new new new double double double double Object Oriented Programming Java - Arrays [5]; [5]; [5]; [5]; R.B. Lenin Winter 2006 DA-IICT 41 Executing the statements on the left in sequence will create the array of arrays shown on the right. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 42 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 43 Two-Dimensional Arrays The expression payScaleTable.length refers to the length of the payScaleTable array itself. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 44 Two-Dimensional Arrays The expression payScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 45 Two-Dimensional Arrays An array that is part of another array is called a subarray. An array of arrays may be initialized when it is created. int numbers = { {1,2,3}, {4,5,6}}; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 46 Two-Dimensional Arrays Subarrays may be of different lengths. Executing triangularArray = new double[4][ ]; for (int i = 0; i < 4; i++) triangularArray[i] = new double [i + 1]; results in an array that looks like: Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 47 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 48 Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 49 ...
View Full Document

This note was uploaded on 09/15/2010 for the course CSE IT115 taught by Professor Rblenin during the Spring '10 term at Punjab Engineering College.

Ask a homework question - tutors are online