1

I am writing a program that stores an ArrayList of Person objects (input is from a text file).

This is the code for the Person class, which I will create Person objects from:

import java.io.Serializable;

public class Person implements Comparable<Person>, 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: <firstName> <lastName> Age: <age>
 */
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 < p
 * @return 0 if this == p
 * @return positive number if this > p
 */
public int compareTo(Person p) {
          return ((Integer)this.getAge()).compareTo(p.getAge());
}

And I created a Comparable interface:

public interface Comparable<T> {
public int compareTo(T o);
}

And here is the code for the class called Collection which will create an ArrayList to store Person objects, I ommitted parts of the code that were not relevant as it is long:

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<Person> people;

public Collection()
{
    people = new ArrayList<Person>();
}   

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<Person> 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);
}
}

However I get this error, the sort does not work:

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 >

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..

0

1 Answer 1

6

And I created a Comparable interface: public interface Comparable { public int compareTo(T o); }

You are not supposed to create your own interface. Use java.lang.Comparable<T>, the one that the Collections.sort() method expects your object to implement

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I deleted the interface... and now sort finally works! Could you explain why I didn't need to create this interface?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.