Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort a collection using Collection.sort()
    primarykey
    data
    text
    <p>I am writing a program that stores an <code>ArrayList</code> of <code>Person</code> objects (input is from a text file).</p> <p>This is the code for the <code>Person</code> class, which I will create <code>Person</code> objects from:</p> <pre><code>import java.io.Serializable; public class Person implements Comparable&lt;Person&gt;, Serializable { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName.toUpperCase(); this.age = age; } public int getAge() { return age; } public String getName() { return firstName; } /** * @return a String of the details of a person in the format: * Name: &lt;firstName&gt; &lt;lastName&gt; Age: &lt;age&gt; */ public String toString() { return "Name: " + firstName + "" + lastName + "\t\t" + "Age: " + age; } /** * Compare the age of the current instance of Person to another age of the specified Person * @return negative number this &lt; p * @return 0 if this == p * @return positive number if this &gt; p */ public int compareTo(Person p) { return ((Integer)this.getAge()).compareTo(p.getAge()); } </code></pre> <p>And I created a <code>Comparable</code> interface:</p> <pre><code>public interface Comparable&lt;T&gt; { public int compareTo(T o); } </code></pre> <p>And here is the code for the class called <code>Collection</code> which will create an <code>ArrayList</code> to store <code>Person</code> objects, I ommitted parts of the code that were not relevant as it is long:</p> <pre><code>import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; import java.util.Iterator; public class Collection { private ArrayList&lt;Person&gt; people; public Collection() { people = new ArrayList&lt;Person&gt;(); } public void readFromFile(String filename) { // code that will get input to assign values to fields to a Person Person newPerson = new Person(firstNameToken, lastNameToken, ageToken); } /** * Prints the details of each person held in the people ArrayList */ public void printDetails() { Iterator&lt;Person&gt; it = people.iterator(); while(it.hasNext()) { Person p = it.next(); System.out.println(p.toString()); } } public static void main(String [] args) throws FileNotFoundException { Collection c = new Collection(); // check //for(Person person : c.people) //{ // System.out.println(person); //} Collections.sort(c.people); } } </code></pre> <p>However I get this error, the sort does not work:</p> <blockquote> <p>Exception in thread "main" java.lang.Error: Unresolved compilation problem: Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The inferred type Person is not a valid substitute for the bounded parameter ></p> </blockquote> <p>Does anyone know why? I am looking furiously around on google for solutions, I can't see what I am missing. I've implemented comparable..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload