aspose file tools*
The moose likes Beginning Java and the fly likes Really need help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Really need help!" Watch "Really need help!" New topic
Author

Really need help!

Andrew Gray
Greenhorn

Joined: Oct 22, 2009
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Have you imported the classes from java.util?
Andrew Gray
Greenhorn

Joined: Oct 22, 2009
Posts: 9
Yeah. import java.util.*; is at the top of the code, just forgot to paste it in.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
By the way: are you returning something from every path through that overridden add method?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Oh, I see you are at ******University. I'm at Teesside. Good thing you're not at S*******d
Andrew Gray
Greenhorn

Joined: Oct 22, 2009
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Try add(E obj)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 22, 2009
Posts: 9
Yeah that fixed the add part. Now I'm still getting the cannot find symbol - compareTo(E)
Andrew Gray
Greenhorn

Joined: Oct 22, 2009
Posts: 9


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

Joined: Jan 28, 2003
Posts: 4181
    
  21

You declare a as an Object, which doesn't have a compareTo method. You should see if you can define it as an E.


Steve
Andrew Gray
Greenhorn

Joined: Oct 22, 2009
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You forgot the Override annotation.
Andreas Hollmann
Greenhorn

Joined: Jan 06, 2010
Posts: 27
deleted
Andreas Hollmann
Greenhorn

Joined: Jan 06, 2010
Posts: 27
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Really need help!