Sunday, 18 August 2013

Generics and ArrayList Help please

Generics and ArrayList Help please

How can i access the methods in the Students class. when i print myList it
uses the toString in the students class, i'm not sure how to access the
other methods. can someone help? is that even possible? help please.
ps i already have the try and catch in my program, i just didn't post here
to make the code shorter
public class DatabaseAccess <T> {
public T[] database;
public ArrayList<T>myList = new ArrayList<T>();
public ArrayList<T> testlist = new ArrayList<T>();
public void userInterface(){
int count = 1;
for (T i : myList){
System.out.println(count++ + ": " + i);
}
}
public void readDatabase(){
try {
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("grocery.bin"));
database = (T[]) in.readObject();
for (int i = 0; i < database.length; i++){
myList.add(database[i]);
}
myList.add((T) "\n");
myList.add((T) "\tStudents:");
ObjectInputStream in1 = new ObjectInputStream(new
FileInputStream("students.bin"));
database = (T[]) in1.readObject();
for (int i = 0; i < database.length; i++){
myList.add(database[i]);
}
}
}
public class Student implements Serializable, Comparable{
private String name, id, address;
private double gpa;
public Student(String name, String id, double gpa){
this.name = name;
this.id = id;
this.gpa = gpa;
}
public int compareTo(Object object){
Student student = (Student ) object;
if(this.gpa < student.getGpa())
return -1;
else
if(this.gpa > student.getGpa())
return 1;
else
return 0;
}
public String getName(){ return name;}
public String getId(){ return id;}
public double getGpa(){ return gpa;}
public String toString(){ return (String.format("%16s%10.4s%8s",
name, id, gpa));}
}

No comments:

Post a Comment