• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Really need help!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++;
}


}
}
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you imported the classes from java.util?
 
Andrew Gray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. import java.util.*; is at the top of the code, just forgot to paste it in.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: are you returning something from every path through that overridden add method?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try add(E obj)
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot the Override annotation.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deleted
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic