Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Really need help!

 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to override the add(obj) method in the linkedlist class, but I keep getting cannot find symbol errors. What am I doing wrong?

public class SortedList<E extends Comparable<? super E>> extends LinkedList<E>
{


public SortedList()
{

}

public boolean add(Object obj)
{
Object a = null;
int count = 0;
if (size() == 0)
{
super.add(obj);
}
while (count < size())
{
a = super.get(count);
if (a.compareTo(obj) < 0)
{
super.add(count-1, obj);
return true;
}else
{
super.add(count, obj);
}
count++;
}


}
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you imported the classes from java.util?
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. import java.util.*; is at the top of the code, just forgot to paste it in.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always give the exact code (unless it is long) and use the code button and give the full details of the error message. Then we can see what's going on.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: are you returning something from every path through that overridden add method?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see you are at ******University. I'm at Teesside. Good thing you're not at S*******d
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Full code:

import java.util.*;

public class SortedList<E extends Comparable<? super E>> extends LinkedList<E>
{


public SortedList()
{

}

public boolean add(Object obj)
{
Object a = null;
int count = 0;
if (size() == 0)
{
super.add(obj);
}
while (count < size())
{
a = super.get(count);
if (a.compareTo(obj) < 0)
{
super.add(count-1, obj);
return true;
}else
{
super.add(count, obj);
}
count++;
}


}
}


error message: cannot find symbol - method add(java.lang.Object). this is the error i'm getting now after adding the first if statement 'if (size() ==0)', before I was getting the same more or less the same error, but replace add(java.lang.Object) with compareTo(java.lang.Object).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try add(E obj)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually try this

@Override public boolean add(E obj)
{
. . .
}

I have a suspicion that your if-else will end up with you adding the element several times. It is difficult to tell because you didn't use the code button and your indentation looks wrong, but I think you will add obj several times in the else.
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that fixed the add part. Now I'm still getting the cannot find symbol - compareTo(E)
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now you've pointed it out, yeah I think it will just add the element several times in the else part. I'm going to have to change that, but I can't work out the error I'm getting.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declare a as an Object, which doesn't have a compareTo method. You should see if you can define it as an E.
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome dude, that's fixed the error! Now I just need to sort the else statement out and I'm done.

Many thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot the Override annotation.
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deleted
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Gray wrote:I'm trying to override the add(obj) method in the linkedlist class, but I keep getting cannot find symbol errors. What am I doing wrong?

....


Hi, SortedList encapsulates the sorting-aspect and is implemented as decorator in happy-collections library (Apache License 2.0).
The example below shows how you can convert any list to a SortedList by decorating it:


The SortedList decorator supports two strategies (1)"Array" good for ArrayList or TreeList and (2)"Linked" good for LinkedList (read the javadoc). The strategies are important if you want to improve the performance, here you can find a benchmark where the TreeList combined with SortedList is ten times faster as normal....

:-)
Andrej
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic