Example of Array List using object stud with data type Student
public class Student
{
private int ID;
private String name;
private int part;
private double cgpa;
public Student()
{ ID=-1;
name=" ";
part=-1;
cgpa=-1.0;
}
public Student(int ID, String name,int part, double cgpa)
{ [Link]=ID;
[Link]=name;
[Link]=part;
[Link]=cgpa;
}
public void setStudent(int i,String n, int p, double c)
{ ID=i;
name=n;
part=p;
cgpa=c;
}
public int getID(){return ID;}
public String getName(){return name;}
public int getPart(){return part;}
public double getCGPA(){return cgpa;}
public String toString()
{ return("\nID:"+ID+"\nName:"+name+"\nPart:"+part+"\nCGPA:"+cgpa);}
}
1
import [Link].*;
public class StudAppArrList
{
public static void main(String args[])
{
ArrayList<Student> data=new ArrayList<Student>();
Scanner inputText=new Scanner([Link]);
Scanner input=new Scanner([Link]);
int id,part;
String name;
double cgpa;
//enter detail for 3 students
for(int i=0;i<3;i++)
{ [Link]("Enter ID:");
id=[Link]();
[Link]("Enter name:");
name=[Link]();
[Link]("Enter part:");
part=[Link]();
[Link]("Enter CGPA:");
cgpa=[Link]();
Student stud=new Student(id, name, part,cgpa);
[Link](stud);
}
//display all the students data
[Link]("Details of all students:");
Student stud=null;
for (int i=0; i<[Link]();i++)
{ stud = [Link](i);
[Link]([Link]());
}
//to display detail of the best student
double highCGPA=0;
int index=0;
for(int i=0;i<[Link]();i++)
{ stud=[Link](i);
if([Link]()>highCGPA)
{ highCGPA=[Link]();
index=i;
}
}
stud=[Link](index);
[Link]("Data for highest student:"+[Link]());
2
//to count the number of students in part 4
int count=0;
for(int i=0;i<[Link]();i++)
{ stud=[Link](i);
if([Link]()==4)
count++;
}
[Link]("There are "+count+" student in part 4");
}
}