Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: bound mismatcherror?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default bound mismatcherror?

    public class Tester
    {
     
    public static void main(String[]args)
    {
     
    ArrayList<BankAccount> balance = new ArrayList<BankAccount>();
     
     
    balance.add(new BankAccount(1000));
    balance.add(new BankAccount(1500));
    balance.add(new BankAccount(750));
    balance.add(new BankAccount(2350));
    balance.add(new BankAccount(525));
     
    Collections.sort(balance); // this is the error line
    }
     
     
     
    public interface Comparable <BankAccount>
    {
     
    int compareTo(Object other);
     
    }

    public class BankAccount implements Comparable <BankAccount>
    {
    ..... etc
    }
    Can anyone tell me what the problem is?

    Description
    Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<BankAccount>). The inferred type BankAccount is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>line 44 Java Problem

    Thanks.


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: bound mismatcherror?

    1. You should reread the Generics section of the java tutorials. Generics can get complex, and it doesn't hurt to read this section many times to ensure you understand them and are using them correctly.

    2. The sort method of Collections has this signature "public static <T extends Comparable<? super T>> void sort(List<T> list)"

    Since it looks like you are declaring your own interface named Comparable (instead of using java.lang.Comparable) you are not satisfying these requirements. Get rid of the interface you created, and use the Comparable interface that is part of the java.lang. package

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bound mismatcherror?

    Thanks you, it helped me solve my problem.

  4. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: bound mismatcherror?

    Please mark this thread solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes