Array of objects in Java

Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can be accessed by the index number as:  a[2]

Syntax:

data_type[] array_name = new data_type[number of elements];

Example:

int[] name = new int[10];

Array of objects

Objects are instance of a class.

Object can easily be created using following syntax: class_name object_name = new class_name();

                                                                     Example: Student name = new Student();

but what if we need to create hundreds of objects of the same class. However this can be achieved using above procedure but imagine the the efforts and time required. To accomplish this task, Java introduces array of objects.

syntax: class_name[] object_name = new class_name[number of objects];

Example: Student name = new Student[5];

this code segment will create 5 objects called 'name' of type Student.

here is a beautiful example of apllication of array of objetcs: Students marksheet

Programs states:

Write a java program to accept details of students such as name, ID no, marks in Maths, Physics, and Chemistry. Display the students detail in descending order of their total marks. (Use array of objects)

import java.io.*;         //importing input-output files
class Student
{
   String name;                     //declaration of variables
   int id;
   int mathmarks;
   int phymarks;
   int chemmarks;
   int total;

   Student(String naam,int idno, int m,int p,int c)          //Initializing variables to user data
   {
          name=naam;
          id=idno;
          mathmarks=m;
          phymarks=p;
          chemmarks=c;
          total=m+p+c;
    }
    void display()                             //displaying information
    {
           System.out.println(name+"\t"+id+"\t"+mathmarks+"\t"+phymarks+"\t"+chemmarks+"\t"+total);
    }

}

class Studentexe                     //main class
{
  public static void main(String args[]) throws IOException                  //exception handling
  {
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         int n=Integer.parseInt(in.readLine());

         Student[]  S=new Student[n];                      // array of objects declared and defined
         for(int i=0;i         {
            
               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String nm=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("Maths marks: ");              
               int m=Integer.parseInt(in.readLine());
               System.out.println("Physics marks: ");
               int p=Integer.parseInt(in.readLine());
               System.out.println("Chem marks: ");
               int c=Integer.parseInt(in.readLine());
             
              S[i]=new Student(nm,idno,m,p,c);                           //calling Student constructor
            
          }
       
         Student temp;                           //swaping to achieve decsending order of total marks
         for(int a=0;a         {
                 for(int b=0;b                 {
                      if(S[b].total                      {
                           temp=S[b];
                           S[b]=S[b+1];
                            S[b+1]=temp;
                       }
                }
        }
        System.out.println("\nName"+"\t"+"ID"+"\t"+"Math"+"\t"+"Phy "+"\t"+"Chem"+"\t"+"Total");            //printing data on the output screen
        for(int i=0;i        {
               S[i].display();
        }

 

     }    //main ends here
}        //Studentexe class ends here

 

Note: 1) save the above code as 'Studentexe.java' while executing.

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Do you feel that TV serials nowadays are having great influence on our personal lives and our thoughts?

Yesterday I was watching a TV serial and there was a situation in it similar to my life and I got tensed that what if it will happen with me. But then I realized that its just a TV serial.
No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 5 answer(s).
/polls/movies-and-entertainment/6584-do-you-feel-that-tv-serials-nowadays-are-having-great-influence-on-our-personal-lives-and-our-thoughts.json?task=poll.vote
6584
radio
[{"id":"22907","title":"Yes","votes":"22","type":"x","order":"1","pct":34.38,"resources":[]},{"id":"22908","title":"No","votes":"4","type":"x","order":"2","pct":6.25,"resources":[]},{"id":"22909","title":"somewhere it is inflencing our lives but not completely","votes":"22","type":"x","order":"3","pct":34.38,"resources":[]},{"id":"22910","title":"I dont watch TV serials","votes":"15","type":"x","order":"4","pct":23.44,"resources":[]},{"id":"22911","title":"Dont know","votes":"1","type":"x","order":"5","pct":1.56,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Whenever I hear about any paranormal incidents or any strange occurrences, the first question that comes to my mind is - did that really happen? Sometimes,
I am hopeful despite all the exploitation My question is from the younger generation that can remember the beginning of this century or they were old
This article is focused particles on chemical evolution. Earth developed around 4.6 billion years back by condensation of elm or cloud of cosmic dust and
Introduction:  When unusually large amount of surface water with the advent of sudden burst of rain comes flooding with force to land area is called
Introduction: The Earth is unique, with the presence of an atmosphere, and life. Many scientific inventions have not yet been enough to find a single