Professional Documents
Culture Documents
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
■ 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
import javax.swing.JOptionPane;
//create object
cloth[i] = new Laundry(); //create empty object using const without param
}
//Display All objects
System.out.println("Object no "+(i+1));
System.out.println(cloth[i]+"\n");
if(cloth[i].getCleanType().equalsIgnoreCase("dry clean"))
System.out.println(cloth[i].getCustName());
}
//find the highest weight of cloth
maxWeight = cloth[i].getClothWeight();
location = i;
}}
}}
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]+ " ");
}
}