Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:19:26+00:00 2026-06-17T20:19:26+00:00

I am writing a program that stores an ArrayList of Person objects (input is

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:19:27+00:00Added an answer on June 17, 2026 at 8:19 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a program that scans a text file for the byte offset of
I am writing a program that stores data in a dictionary object, but this
I'm writing a program in Python that stores information for every file on the
Looking for help on writing a Perl program that takes an input file and
I'm writing a program that creates an object which holds to inputs from the
I'm writing a program that displays products and the stores at which they can
I'm writing a personal wiki-style program in Python that stores text files in a
Im writing a program that should read input via stdin, so I have the
I'm writing a program that reads huge file (3x280 GB) and does a fitting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.