WTF persian cat
WTF persian cat

Reputation: 11

linked list of linked lists java

so I'm making an app, and I need to use linked list of linked lists. this is my current code:

public class server{
private List <Group> serverList= new List <Group>();

now, in the class "group":

List <user> groupList= new List <user>();
groupList(1)=new Node (null, T x);

and, it just doesen't work... after I'll get this done, it will be easyer to use the other Node abilities. I'm just a High School student so don't judge me. I'm using the unit4 pack

edit unit4 is an expension pack to Eclipse, made by the Ministry of Education in Israel. The pack lets you use Node, List, Stack, Queue, and BinTreeNode without being have to actually write them down every time.

Upvotes: 0

Views: 979

Answers (1)

cyberbrain
cyberbrain

Reputation: 5075

I suppose, your List<User> is a java.util.List If this is correct, then this is an interface - just a kind of "contract" that a class can assure to its users.

You cannot instanciate an interface, just an implementation of that interface - java.util.LinkedList for example.

On the left side of an assignment, the most generic interface possible is a good thing, on the right side of the assignment you have to specify your wanted implementation of that interface. In your case this could look like

import java.util.List;
import java.util.LinkedList;


List<User> = new LinkedList<>();

(I left out the second <User> intentionally and just wrote <> there since from Java version 7 on it is allowed and good practice to increase readability.)

Next error in your code is, that you cannot set List elements with

groupList(1)=new Node (null, T x);

The method to assign elements is public E set(int index, E element) where E is the type of the list elements. So your code would look like

groupList.set(1, new Node(null, T x));

which still has some more errors in it.

First of all, lists start at index 0 (zero), so to fill in the first element, you would call groupList.set(0, ...) which is not correct again. Set only replaces already existing elements, but would throw an exception if the index is out of the existing bounds of the list. To add new elements you have to use method public void add(int index, E element).

groupList.add(new Node(null, T x));

This adds a new object of class Node to the end of your (at that moment empty) list.

Still an error in it - the call of new Node(null, T x). Seems if you copied that out of a documentation or declaration, but did not fill in all of the parameters correctly. The second parameter (T x) shows that the constructor expects some object of type T (which is kind of a generic variable as with the parameter User in List<User>).

As you did not give us the package of Node in your question (I don't know the "uni4 pack", so maybe the solution is in there) I cannot help you much there.

You could enter null as parameter there too - if that is sufficient for your needs.

groupList.add(new Node(null, null));

In case the compiler won't compile that, you could cast the second parameter to a type:

groupList.add(new Node(null, (String)null));

Looks strange, but sometimes the compiler needs a hint ;-).

If you update your question with more details on the problem you have to solve, I could give you more specific help here.

Upvotes: 1

Related Questions