java - Sorting through ArrayList of Objects, Add given Instance to ArrayList of given instances issue -
i'm getting error
no suitable method found add(object), method collected.add(student not applicable)
when try following code. swear i've done way before? confused. appreciate insights. cheers
//sort , display list of student objects sortby (surname or id) public static void sortanddisplaystudents(string sortby, arraylist<object> objlist) { arraylist<student> students = new arraylist<>(); //add objcets in objlist student for(object s: objlist) { if(s instanceof student) { students.add(s); } } }
just avoid errors, change way construct array list:
arraylist<student> students = new arraylist<>(); arraylist<student> students = new arraylist<student>();
as add method, want cast passing object otherwise jvm not know object being passed instanceof
student:
students.add((student) s);
also, should ide, solve these problems before happen.
wiki
Comments
Post a Comment