java - Error when using Collections.sort() -


i'm wondering why i'm getting error after trying sort list. error happens when try sort list containing "student" objects.

import java.lang.reflect.array; import java.util.*; import java.util.arraylist;  public class mainprogram {     public static void main(string[] args) {         list<student> classstudents = new arraylist<student>();      //add 4 students list         classstudents.add(new student("charles", 22));         classstudents.add(new student("chris", 25));         classstudents.add(new student("robert", 23));         classstudents.add(new student("adam", 21));      //sort , print         collections.sort(classstudents);    //why not work?         system.out.println("classstudent(sorted) ---> "         + arrays.tostring(classstudents.toarray()));     } } 

class student implements comparable<student>{     // fields     private string name;     private int age;      //constructor     public student(string name, int age){          name = name;         age = age;         }     //override compare     public int compareto(student otherstudent){         if(this.age == otherstudent.age){             return 0;         } else if(this.age < otherstudent.temp){             return -1;         } else{             return 1;         }     }      //override tostring     public string tostring(){         return this.name + " " + this.age;     } } 

interface comparable<student>{     int compareto(student otherstudent);      string tostring(); } 

the error is:

bound mismatch: generic method sort(list<t>) of type collections not applicable arguments (list<student>). inferred type student not valid substitute bounded parameter <t extends comparable<? super t>> 

i not understand error means , how fix it. input appreciated.

it may not obvious based on error message, if take @ documentation of collection.sort fill find method (like rest of standard api methods) expects list of instances implementing already defined in api interface in case java.lang.comparable.

so don't try create own comparable interface. remember if method expects type, type must defined somewhere (otherwise class such method wouldn't compile because method can't use unknown/undefined type).

also comparing method defined in java.util.comparable compareto not compareto (java case sensitive these methods not treated equal).


other problems

  • student doesn't have temp field

    } else if (this.age < otherstudent.temp) { 

    should be

    } else if (this.age < otherstudent.age) { 

    or better, instead of age comparing conditions if(..<..){-1} else if(..==..){0} else {1} use

    return integer.compare(age, otherstudent.age); 
  • in constructor need this.name determine name refering to. this.name means field of instance, name reference variable passed constructor. need

    public student(string name, int age) {     this.name = name;     this.age = age; } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -