Results 1 to 5 of 5
  1. #1
    repairmanjack is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Help with comparing generic types

    post solved
    Last edited by repairmanjack; 09-22-2009 at 08:50 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I am no expert in things like this, far from it, but shouldn't the object held by your list implement the Comparable interface? I think that you declare this something like so:
    Java Code:
    public class OrderedList<E extends Comparable<E>> implements List<E>, Iterable<E> {
      //....
    (I used List here rather than ListInterface since I have no idea what ListInterface is).

  3. #3
    repairmanjack is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Yes, but for this project I was given the test program and was told not to change it. implementing the Comparable interface causes an error in the test program.

    Bound mismatch: The type T is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type OrderedList<E>

  4. #4
    repairmanjack is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    I think that I have solved the problem. But now I can't get my add method to work. Here is what I am using:

    Java Code:
      
    when I run the program it adds the first item to the list then it gives me the following error:

    Exception in thread "main" java.lang.NullPointerException
    at java.lang.Character.compareTo(Unknown Source)
    at TestOrderedList$1.compare(TestOrderedList.java:24)
    at TestOrderedList$1.compare(TestOrderedList.java:1)
    at OrderedList.add(OrderedList.java:34)
    at TestOrderedList.test0(TestOrderedList.java:30)
    at TestOrderedList.main(TestOrderedList.java:7)
    Last edited by repairmanjack; 09-22-2009 at 08:50 AM.

  5. #5
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    Incorrect code:

    PHP Code:
    		for (int i = 0; add == true;i++)
    			if (compare(newEntry,list[i])>=0){
    				list[size] = newEntry;
    				add = true;}
    			
    			size++;
    		
    		return true;
    1. add is ALWAYS true, it never changes to false.
    2. list[size] = newEntry;. YOu should to insert new element in ith position.
    Try Controls4J - Advanced Swing Components.

Similar Threads

  1. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 11:29 PM
  2. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 11:27 AM
  3. Generic methods
    By andre1011 in forum Advanced Java
    Replies: 7
    Last Post: 02-25-2009, 03:17 PM
  4. Can generic types implement an interface?
    By jdgallag in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 05:07 AM
  5. Comparing types, integer with null
    By Felissa in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •