You are on page 1of 18

CHAPTER 1: ARRAY

OF OBJECTS
ADVANCED OBJECT–ORIENTED PROGRAMMING
Introduction to Arrays

■ Concept : An array can hold multiple values of the same data type simultaneously
■ An array is an object that can store a group of values, all of same type.
■ Creating and using an array in Java is similar to creating and using any other type of
object.
■ You declare a reference variable and use the new key word to create an instance of the
array in memory.
■ Example : int [ ] numbers = new int [6];
■ The number inside the brackets is the array’s size declaratory. It indicates the number of
elements, or values, the array can hold.
numbers variable numbers reference an array with enough memory for 5 int values

Element 0 Element 1 Element 2 Element 3 Element 4

■ Access the element in array by using index/subscript. A subscript is used as an index to pinpoint a
specific element within an array.
■ Subscript numbering always start with zero.
■ E.g. numbers[0] = 20;
Inputting an Outputting Array content
import java.util.Scanner;
public class ArrayDemo1
{
Scanner keyboard = new Scanner(System.in);
int [] hours = new int [3];
System.out.print(“Employee no 1”);
hours [0] = keyboard.nextInt();
System.out.print(“Employee no 2”);
hours [1] = keyboard.nextInt();
System.out.print(“Employee no 3”);
hours [2] = keyboard.nextInt();
//display value entered
System.out.println(“The hours you entered are:”);
System.out.println(hours[0]);
System.out.println(hours[1]);
System.out.println(hours[2]);
}
}
Simplify the code!

import java.util.Scanner;
public class ArrayDemo1
{
Scanner keyboard = new Scanner(System.in);
int [] hours = new int [3];
for(int i = 0; i<3; i++)
{
System.out.print(“Employee no”+(i+1));
hours [i] = keyboard.nextInt();
}
//display value entered
System.out.println(“The hours you entered are:”);
for(int i = 0; i<3; i++)
{
System.out.println(hours[i]);
}
}
}
Note: Use for loop to execute the process in array.
Arrays of objects

■ You may create arrays of objects that are instances of classes that you have written
■ Recall we have a class Student.
■ An array of Student objects could be created to represent all the students objects.
■ The following code declares an array of 5 Student objects
Student [] stud = new Student [5];
■ the following statement show how to use loop to create an object of each elements
for(int i = 0; i<stud.length; i++)
{
stud [i] = new Student();
}
address
stud [0] null
This is an array
stud [1] null representation diagram for
stud [2] null Student array of objects. The
stud variable holds the
stud [3] null address of a Student array
stud [4] null
Example

■ From a previous chapter, we have a class Laundry


■ Modify the main class which consist of the following statement
– Create an array of 10 Laundry objects
– Ask user to enter data and store it into objects
– Display All objects
– Display objects which cleaning type is dry clean.
– Find the highest weight of cloth and display the customer name and the weight.
Answer
//this is a main class of class Laundry

//this class will demonstrate how to use an array

import javax.swing.JOptionPane;

public class TestLaundry1

public static void main(String [] args)

//create array of object Laundry

Laundry [] cloth = new Laundry[3];;


//Ask user to enter data and store it into objects

for(int i = 0; i<cloth.length; i++)

//create object

cloth[i] = new Laundry(); //create empty object using const without param

//ask user to enter data

String custName = JOptionPane.showInputDialog("Enter name");

double clothWeight = Double.parseDouble(JOptionPane.showInputDialog("Enter cloth


weight"));

String cleanType = JOptionPane.showInputDialog("Enter cleaning type");

//store data into object array

cloth[i].setLaundry(custName, clothWeight, cleanType);

}
//Display All objects

for(int i = 0; i<cloth.length; i++)

System.out.println("Object no "+(i+1));

System.out.println(cloth[i]+"\n");

//display name of customer who choose dry clean as cleaning type

System.out.println("Name of customer who choose dry clean ");

for(int i = 0; i<cloth.length; i++)

if(cloth[i].getCleanType().equalsIgnoreCase("dry clean"))

System.out.println(cloth[i].getCustName());

}
//find the highest weight of cloth

double maxWeight = -999.0; //set as benchmark to compare

int location = 0; //variable to get the index of object found

for(int i = 0; i<cloth.length; i++)

if(cloth[i].getClothWeight() > maxWeight)

maxWeight = cloth[i].getClothWeight();

location = i;

}}

System.out.println("Customer who has the most cloth weight is


"+cloth[location].getCustName()+

"and the weight is "+cloth[location].getClothWeight());

}}
Returning an array from methods

■ A method can return a reference of an array.


■ The return type of a method must be declared as an array of the correct data type.
Returning an array from methods

EXAMPLE 1

 java.util.Arrays;  
public class ReturnArrayExample1  
{  
public static void main(String args[])  
{  
int[] a=numbers();           //obtain the array  
for (int i = 0; i < a.length; i++) //for loop to print the array  
System.out.print( a[i]+ " ");     
}  
public static int[] numbers()  
{  
int[] arr={5,6,7,8,9};  //initializing array  
return arr;  
}  
}  
Passing arrays to methods
1.3 Returning an array from methods
EXAMPLE 2
 import java.util.Arrays;  
class ReturnArrayExample3  
{  
public static int[] returnArray()  
{  
int a1=20;  
int a2=23;  
int a3=87;  
return new int[] {a1,a2,a3};     //returns array  
}  
public static void main(String args[])  
{  
int[] arr=returnArray();    //invoking method and storing returned array into arr  
System.out.println(Arrays.toString(arr));   //returns the string representation of the object  
}  
Passing arrays to methods
EXAMPLE 3
 public class ReturnArrayExample3  
{  
public static double[] returnArray( )  
{  
double[] arr = new double [3];    // Creating an array of 3 elements  
arr[0]=6.9;  
arr [1]=2.5;  
arr [2]=11.5;  
return( arr );            // Return the reference of the array  
}  
public static void main(String[] args)  
{  
double[] a;         //variable to store returned array  
a = returnArray();      //called method  
for (int i = 0; i < a.length; i++) //for loop to print the array  
System.out.println( a[i]+ " ");     
}  
}  

You might also like