Array of Objects in Java
Introduction to Array of Objects
Sorting an Array of Objects
Introduction to Array of objects in Java
An array of objects in Java is a collection of objects of the same type. It is a more powerful and flexible data structure than a traditional array, which can only store primitive data types.
To create an array of objects, you first need to declare an array of object references. Then, you can initialize each element of the array with a new object. For example, the following code creates an array of `Person` objects:
Person[] people = new Person[3]; people[0] = new Person("Alice", 25); people[1] = new Person("Bob", 30); people[2] = new Person("Carol", 35);
Once you have created an array of objects, you can access the elements of the array using the square bracket notation. For example, the following code prints the name of the first person in the `people` array to the console:
System.out.println(people[0].name);
Output:
Alice
You can also use loops to iterate over an array of objects. For example, the following code prints the names of all the people in the `people` array to the console:
for (int i = 0; i < people.length; i++) { System.out.println(people[i].name); }
Output:
Alice Bob Carol
Arrays of objects are a powerful tool that can be used to store and manage data in a variety of ways. For example, you could use an array of objects to store the information for a customer database, the products in an online store, or the students in a class.
Here are some of the benefits of using arrays of objects in Java:
- Flexibility: Arrays of objects can store any type of object, which makes them very flexible.
- Performance: Arrays of objects can be very efficient for accessing and manipulating data.
- Reusability: Arrays of objects can be reused in different parts of your program, which can save you time and effort.
Arrays of objects are an essential part of Java programming. By understanding the basics of arrays of objects, you can write more efficient and maintainable code.
Exercises
Code for creating an array of student objects and invoking its methods
import java.util.Scanner;
public class javalabclass
{
public static void main(String args[])
{
//declaring an array of objects for a class
student[] java = new student[3];
//allocation of memory for student objects
for(int i=0;i < java.length;i++)
{
java[i]=new student();
}
//calling the methods for each object
for(int i=0;i < java.length;i++)
{
java[i].set_profile();
java[i].view_profile();
}
}
}
class student
{
String regNumber;
String Name;
String city;
public void set_profile()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter your regNumber");
this.regNumber = input.next();
System.out.println("Enter your Name");
this.Name = input.next();
System.out.println("Enter your City");
this.city=input.next();
}
public void view_profile()
{
System.out.println(this.Name+this.regNumber+this.city);
}
}
Output:
Enter your regNumber
112
Enter your Name
Prince
Enter your City
Vellore
Prince112Vellore
Enter your regNumber
113
Enter your Name
Mathew
Enter your City
Pune
Mathew113Pune
Enter your regNumber
114
Enter your Name
Satish
Enter your City
Mumbai
Satish114Mumbai
Code for Searching an Array of Student Objects using an input Student Name
import java.util.Scanner;
public class javalabclass
{
public static void main(String args[])
{
//declaring an array of objects for a class
student[] java = new student[3];
//allocation of memory for student objects
for(int i=0;i < java.length;i++)
{
java[i]=new student();
}
//calling the methods for each object
for(int i=0;i < java.length;i++)
{
java[i].set_profile();
java[i].view_profile();
}
//calling the search method
Scanner input = new Scanner(System.in);
System.out.println("Enter the name to search");
String searchName = input.next();
student.search(java,searchName);
}
}
class student
{
String regNumber;
String Name;
String city;
public void set_profile()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter your regNumber");
this.regNumber = input.next();
System.out.println("Enter your Name");
this.Name = input.next();
System.out.println("Enter your City");
this.city=input.next();
}
public void view_profile()
{
System.out.println(this.Name+this.regNumber+this.city);
}
public static void search(student t[],String inputName)
{
boolean found =false;
for(int i=0;i < t.length;i++)
{
if(t[i].Name.equals(inputName))
{
System.out.println(t[i].regNumber+t[i].city);
found=true;
break;
}
}
if(!found)
{
System.out.println("No such student in the array");
}
}
}
Output:
Enter your regNumber
112
Enter your Name
satish
Enter your City
vellore
satish112vellore
Enter your regNumber
113
Enter your Name
Prince
Enter your City
Mumbai
Prince113Mumbai
Enter your regNumber
114
Enter your Name
Mathew
Enter your City
Jaipur
Mathew114Jaipur
Enter the name to search
Prince
113Mumbai
Display the details of Employee objects sorted in the ascending order of their name
Data Members
- Employee ID
- Name
- Date of Birth
- Phone
Methods
- Set_EmployeeInfo
- View_Profile
- Sort_Employee(employee s[])
import java.util.*;
public class javalabclass{
public static void main(String args[]){
System.out.println("Enter the number of employees:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Employee[] a=new Employee[n];
for(int i=0;i < n;i++)
{
a[i]= new Employee();
System.out.println("For Employee"+(i+1));
a[i].Set_Employee();
}
//sorting the array of employee objects by name
Employee.sort_employeeName(a);
//viewing the employee data
for(int i=0;i < n;i++)
{
a[i].View_Employee();
}
}
}
class Employee{
public String Employee_id;
public String Name;
public int year;
public int month;
public int date;
public String phno;
public void Set_Employee()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the emplyee ID:");
Employee_id=sc.next();
System.out.println("Enter the Name:");
Name=sc.next();
System.out.println("Enter the year of birth:");
year=sc.nextInt();
System.out.println("Enter the month of birth:");
month=sc.nextInt();
System.out.println("Enter the date of birth:");
date=sc.nextInt();
System.out.println("Enter the phone number:");
phno=sc.next();
}
public static void sort_employeeName(Employee[] a)
{
for(int i=0;i < a.length-1;i++)
{
for(int j=i+1;j < a.length;j++){
if((a[i].Name).compareTo(a[j].Name)>0){
Employee temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
public void View_Employee()
{
Scanner sc=new Scanner(System.in);
System.out.println("The Employee ID is: "+Employee_id);
System.out.println("The Employee Name is: "+Name);
System.out.println("The Employee year of birth: "+year);
System.out.println("The Employee month of birth: "+month);
System.out.println("The Employee date of birth: "+date);
System.out.println("The Employee phone number: "+phno);
}
}
Output:
Enter the number of employees:
3
For Employee1
Enter the emplyee ID:
112
Enter the Name:
Mohan
Enter the year of birth:
2001
Enter the month of birth:
0
Enter the date of birth:
12
Enter the phone number:
8799999
For Employee2
Enter the emplyee ID:
113
Enter the Name:
Anbu
Enter the year of birth:
2002
Enter the month of birth:
3
Enter the date of birth:
12
Enter the phone number:
767777
For Employee3
Enter the emplyee ID:
114
Enter the Name:
pratap
Enter the year of birth:
2000
Enter the month of birth:
4
Enter the date of birth:
11
Enter the phone number:
78888
The Employee ID is: 113
The Employee Name is: Anbu
The Employee year of birth: 2002
The Employee month of birth: 3
The Employee date of birth: 12
The Employee phone number: 767777
The Employee ID is: 112
The Employee Name is: Mohan
The Employee year of birth: 2001
The Employee month of birth: 0
The Employee date of birth: 12
The Employee phone number: 8799999
The Employee ID is: 114
The Employee Name is: pratap
The Employee year of birth: 2000
The Employee month of birth: 4
The Employee date of birth: 11
The Employee phone number: 78888
Search the array of Employee objects to display the names of Employees who will retire after 2030 if the retirement age for an employee is 58 years.
Data Members
- Employee ID
- Name
- Date of Birth (Get Year , Month and Day as integers)
- Phone
Methods
- Set_EmployeeInfo
- View_Profile
- View_RetirementInfo(employee s[])
import java.util.*;
public class javalabclass{
public static void main(String args[]){
System.out.println("Enter the number of employees:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Employee[] a=new Employee[n];
for(int i=0;i < n;i++)
{
a[i]= new Employee();
System.out.println("For Employee"+(i+1));
a[i].Set_Employee();
}
//Viewing Employees who will retire after 2030
Employee.view_retirementInfo(a);
}
}
class Employee{
public String Employee_id;
public String Name;
public int year;
public int month;
public int date;
public String phno;
public void Set_Employee()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the emplyee ID:");
Employee_id=sc.next();
System.out.println("Enter the Name:");
Name=sc.next();
System.out.println("Enter the year of birth:");
year=sc.nextInt();
System.out.println("Enter the month of birth:");
month=sc.nextInt();
System.out.println("Enter the date of birth:");
date=sc.nextInt();
System.out.println("Enter the phone number:");
phno=sc.next();
}
public static void view_retirementInfo(Employee[] s)
{
boolean found = false;
System.out.println("Employees retiring after 2030");
for(int i=0;i < s.length;i++)
{
if((s[i].year+58)>2030)
{
System.out.println(s[i].Name+" "+s[i].phno);
found=true;
}
}
if(!found)
{
System.out.println("No Employees will retire after 2030");
}
}
public void View_Profile()
{
Scanner sc=new Scanner(System.in);
System.out.println("The Employee ID is: "+Employee_id);
System.out.println("The Employee Name is: "+Name);
System.out.println("The Employee year of birth: "+year);
System.out.println("The Employee month of birth: "+month);
System.out.println("The Employee date of birth: "+date);
System.out.println("The Employee phone number: "+phno);
}
}
Output:
Enter the number of employees:
3
For Employee1
Enter the emplyee ID:
11
Enter the Name:
Satish
Enter the year of birth:
1940
Enter the month of birth:
0
Enter the date of birth:
1
Enter the phone number:
766777
For Employee2
Enter the emplyee ID:
12
Enter the Name:
Ram
Enter the year of birth:
2020
Enter the month of birth:
2
Enter the date of birth:
14
Enter the phone number:
6578877
For Employee3
Enter the emplyee ID:
13
Enter the Name:
Prince
Enter the year of birth:
2021
Enter the month of birth:
0
Enter the date of birth:
21
Enter the phone number:
566777
Employees retiring after 2030
Ram 6578877
Prince 566777