post solved
Printable View
post solved
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:
(I used List here rather than ListInterface since I have no idea what ListInterface is).Code:public class OrderedList<E extends Comparable<E>> implements List<E>, Iterable<E> {
//....
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>
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:
when I run the program it adds the first item to the list then it gives me the following error:Code:
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)
Incorrect code:
1. add is ALWAYS true, it never changes to false.Code:for (int i = 0; add == true;i++)
if (compare(newEntry,list[i])>=0){
list[size] = newEntry;
add = true;}
size++;
return true;
2. list[size] = newEntry;. YOu should to insert new element in ith position.