0

First of all i have little experience in Java.

Getting to my question, I am implementing my own list with my own methods.

 public class MyList<E> implements List<E>{

 private Node<E> head;

 private static class Node<E> {
     private E element; 
     .....
 }

Now, there is a method in this class that compares this list with a given one:

public boolean equals(List<E> list){
....
}

To start this method, first we would have to get list's head, so afterward i can access element and compare the list.
Now, we don't have a getHead() method. So, we tried creating the following method in the implementing class

public Node<E> getHead(){    

The compiler does not find the symbol. Of course, we need to declare it first in the interface. The problem is, if we try to do it, interface does not know what Node is. So i am in a deadlock here.

I get the error:

  Cannot find symbol
  symbol  : method getHead()
  location: interface List<E>
    Node<E> lhead = list.getHead();

What i want it to make getHead a implementation-specific method. How do i do that?

I ask, can we move the internal class in the implementing to the interface? Have no idea how to solve this issue,

thanks for your time

7
  • 1
    Where are you trying to put that method? Compiles fine for me Commented Oct 1, 2012 at 21:10
  • Why wouldn't getHead() return E? Commented Oct 1, 2012 at 21:10
  • @MattBall What? head is an instance of Node<E> so getHead should return Node<E>, no? Commented Oct 1, 2012 at 21:11
  • @Alex Coleman I am trying to put it in the implementing class. The thing is, when i use getHead it says symbol not found. Commented Oct 1, 2012 at 21:13
  • 2
    public boolean equals(List<E> list){ Usually equals method doesn't look like that. The parameter type is Object. It overloads the method public boolean equals(Object object){. Try with an @Override annotation. Commented Oct 1, 2012 at 21:15

3 Answers 3

2

Are you implementing java.util.List or some custom List interface you created? If you are using the standard class, of course you cannot modify core JDK class.

If you have a custom List interface, I see few issues:

  • Your equals() method should accept Object and downcast. Otherwise you are overloading instead of overriding:

    public boolean equals(Object obj) {
      //check type first
      List<E> list = (List<E>)obj;
    
  • Node class is defined privately inside MyList. Put it in your List interface and it will be publicly accessible.

  • Finally typically you define equals() in terms of content, not the representation. You might have several List implementations and as long as they hold the same data in the same order, lists should be considered equal. In your implementation only lists using head abstraction are equal. Instead of relying on getHead(), use an iterator and compare the items one-by-one.

Sign up to request clarification or add additional context in comments.

Comments

0

Just out of curiosity, but your list class should have a int getSize() and E get(int index) method (this is usually available in a list). Now, you can just check if the size is equal, if so you can iterate over the list and compare element by element (using the E get(int index) method).

Edit: reference implementation

public boolean equals(List<E> other) {
    boolean equal = (getSize() == other.getSize());
    for(int i = 0; i < getSize() && equal; i++) {
        equal = get(i).equals(other.get(i));
    }
    return equal;
}

5 Comments

i do have the getSize. but i need gethead to be able to compare. for some reason is not compiling
I don't really understand the purpose of a getHead() method because you need to iterate over all elements to compare them anyway. In fact, you can just iterate through the list(s) using the get(...) method and compare the elements itself.
Yes but i have to compare the elements that are inside head, how to I access the head of the other list without the getHead?
@Alessandroempire: I added a reference implementation of what I mean!
Yep, but therefore you have the get() method that should return the element at the asked index. If the index is 0 it should return the first element which is the head (if there is an element). get(0) = head! get(1) = head.next, and so on
0

getHead() is an implementation-specific method, it doesn't belong in the interface. But this also means that you cannot use it as a part of an interface, and you have to call it as a method of the implementing class:

List<String> myList = new MyList<String>();
myList.getHead() // Wrong. Compiler error.

MyList<String> myList = new MyList<String>();
myList.getHead() // Works :)
List<String> asList = myList; // If you want to work with a List from now on

In your case, there's also the issue that equals needs to take an Object, which means that you need to do a type-check if you need to use implementation-specific (or even List-specific) methods:

public boolean equals(Object o)

    if (o == this)
        return true;

    if( o instanceof MyList<E> ){
        MyList<E> myList = (MyList<E>)o;
        // do MyList stuff here ...

    }
    // Things to do if you're comparing something that isn't a `MyList`
    // go here
}

2 Comments

ok ok let me try that. becuase thats the thing, its an implementation-specific method, and for some reason is not compiling.
the thing is i need to use getHead inside a method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.