问题
The array has objects from different subclasses. How can I print the objects according to there subclasses?
I used scanner to read student (Graduate and Undergraduate) objects and put into an array.
I need to use inheritance and toString methods to get the results.
Original data in txt file:
G Steve 23 Biology
U Julia 19 Music Flute, year: Sophomore
G David 25 Math
I was able to read and print the objects but only in original order:
Graduate Name:Steve, Age: 23, Major: Biology
Undergraduate Name: Julia, Age: 19, Major: Music, year: Sophomore
Graduate Name: Dvid, Age: 25, Major: Math
Below is the method I used in tester file:
ArrayList<Student> studentList = new ArrayList<>();
Student s = new Undergraduate(type,name,age,major,year);
studentList.add(s);
Student s = new Graduate(type,name,age, major);
studentList.add(s);
for (Student d:studentList) {
System.out.println(d.toString());
}
=== Expected result:===
Number of Undergraduate Student: 1
Name: Julia, Age: 19, Major: Music, year: Sophomore
Number of Graduate Student: 2
Name:Steve, Age: 23, Major: Biology
Name:Dvid, Age: 25, Major: Math
===
This is my first question here. I am not familiar with the system. I tried to make my question easier to read. Sorry if it looks messy.
回答1:
You can use instanceof to separate the different types of objects. ex.
if (s instanceof Undergraduate){
Undergraduate u = (Undergraduate) s;
}
You can iterate your list and count your objets using instanceof. You can do the check above during the input too.
回答2:
The simplest way of doing this is to create two separate ArrayLists: One for Undergraduate students and one for Graduate students:
ArrayList<Student> undGrdStudentList = new ArrayList<>();
ArrayList<Student> grdStudentList = new ArrayList<>();
if ("U".equals(type)){
Student s = new Undergraduate(type,name,major);
undGrdStudentList.add(s);
}else{
Student s = new Graduate(type,name,major);
grdStudentList .add(s);
}
System.out.println("Number of Undergraduate Students: " + undGrdStudentList.size());
for (Student d:undGrdStudentList) {
System.out.println(d.toString());
}
System.out.println("Number of Graduate Students: " + grdStudentList .size());
for (Student d:grdStudentList ) {
System.out.println(d.toString());
}
Something like that
回答3:
Triple<String,Integer,String> student;
List<Triple<String,Integer,String>> UstudentList = new ArrayList<>();
student = new MutableTriple<>("jin",23,"biology");
UstudentList.add(student);
List<Triple<String,Integer,String>> GstudentList = new ArrayList<>();
student = new MutableTriple<>("amol",23,"music");
GstudentList.add(student);
Triple class is from org.apache.commons.lang3.tuple.Triple
for your expected result
System.out.println("No. of Undergraduate: "+UstudentList.size())
for (Triple<String,Integer,String> stud: UstudentList) {
System.out.println(stud.getLeft()+stud.getMiddle()+stud.getRight());
}
System.out.println("No. of Graduate: "+GstudentList.size())
for (Triple<String,Integer,String> stud: GstudentList) {
System.out.println(stud.getLeft()+stud.getMiddle()+stud.getRight());
}
来源:https://stackoverflow.com/questions/58371462/how-to-separate-different-values-in-2-d-array