OBJECT ORIENTED
PROGRAMMING
ARRAY OF
OBJECTS
OUTLINE
◾ Array of
Objects
◾How to declare and initialize array of
object
Circle circleArray[] = new Circle[3]; //declare
circleArray[0] = new Circle(); //initialize
circleArray[1] = new Circle();
circleArray[2] = new Circle();
ARRAY OF OBJECTS
◾ For example, the following statement declares and creates an array of ten
Circle objects:
◾ Circle circleArray[] = new Circle[10];
◾ Loop To initialize circleArray,
for (int i = 0; i < circleArray.length; i++)
{
circleArray[i] = new Circle();
}
◾ So, invoking circleArray[1].getArea() involves two levels of referencing.
◾ circleArray references the entire array; circleArray[1] references a Circle
object.
◾How to use an array of objects?
Let's Say W e Have A C l a s s N a m e d P e r s o n
Representing I n d i v i d u als W i t h N a m e
A n d A ge: ◾ public class Main {
◾ public static void main(String[] args) {
◾ public class
◾ / Create an array of Person objects to hold 3 persons
Person {
◾ Person peopleArray[ ] = new Person[3];
◾ private String
◾ / Create individual Person objects and store references in
name; the array
◾ private int age; ◾ peopleArray[0] = new Person("Alice", 25);
◾ public Person(String name,
◾ peopleArray[1] = new Person(“Bob", 27); 2nd way to
int age) {
◾ peopleArray[2] = new Person(“Core", 22); do same is on
◾ this.name = name; Next slides
◾ / Accessing and using the objects in
◾ this.age = age;
◾ the array for (Person p : peopleArray)
◾ } ◾ {
Public string getName(){ System.out.println("Name: " + p.getName() + ",
Age: " + p.getAge());
return name; ◾ }
} ◾ }
Initialize All Objects In A n Array At Once Using
Array Initializer
◾ Assuming we have the Person class as defined in the previous example, you can
initialize all objects in
the Person array at once like this:
public class Main {
public static void main(String[] args) {
/ Initialize all objects in the array at once using an array initializer
Person peopleArray[ ]
= { new
Person("Alice", 25),
new Person("Bob",
30),
new Person("Charlie",
22)
};
/ Accessing and using the objects in the
array for (Person person :
INITIALIZE ARRAY BY TAKING INPUT FROM USER
public class Main {
import java.util.Scanner;
public static void main(String[] args) {
public class Person {
Scanner scanner = new Scanner(System.in);
private String name;
System.out.print("Enter the number of people: ");
private int age;
int numPeople = scanner.nextInt();
/ Create an array to hold the people
public Person(String name, int
age) { Person peopleArray[ ] = new Person[numPeople];
/ Take input for each person and create the objects
this.name = name; for (int i = 0; i < numPeople; i++) {
this.age = age; String name = scanner.nextLine();
} int age = scanner.nextInt();
peopleArray[i] = new Person(name, age);
}
scanner.close();
for (Person person : peopleArray) {
System.out.println("Name: " +
person.getName() + ", Age: " +
person.getAge());}
Array of Objects in JAVA- Another
example
Rest of the code is on next line….
Initialize Objects at the Same Time