An array of objects!!
First, we define a Student class:
public Student(String f, String l, int s, double g)
{
first=new String(f);
last=new String(l);
stuNum=s;
gpa=g;
}
public String toString()
{
return first+" "+last+" "+stuNum+" "+gpa;
}
// we compare by last name alphabetical
public int compareTo(Student otherStudent)
{
return last.compareTo(otherStudent.last);
}
}
Now we show how to create an array of Students!
public class ArrayOfStudent
{
public static void main(String [] args)
{
Student [] stuArray=new Student[10]; <--------------------- This is the important line !!!!!!
int nextPlaceForStudent=0;
int k;
stuArray[nextPlaceForStudent]=new Student("bill","smith",12345,3.45);
nextPlaceForStudent++;
stuArray[nextPlaceForStudent]=new Student("sue","davis",31265,3.25);
nextPlaceForStudent++;
stuArray[nextPlaceForStudent]=new Student("mary","adams",48345,3.75);
nextPlaceForStudent++;
stuArray[nextPlaceForStudent]=new Student("dave","harris",98765,2.45);
nextPlaceForStudent++;
for(k=0;k<nextPlaceForStudent;k++)
System.out.println(stuArray[k]);
}
}