0

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.

asked May 1, 2017 at 10:38
4
  • 1
    What does "it just doesen't work" mean? That doesn't tell us what problem you are having Commented May 1, 2017 at 10:40
  • 1
    Since List is an interface, you can't create a new List() .... you should initialize it with one of the classes implementing the List interface. new LinkedList<>();, for example, should work Commented May 1, 2017 at 10:41
  • Thanks, helped me a lot! It stopped screaming on me. Now, working with the list will be much easier. Commented May 1, 2017 at 11:07
  • I would recommend using ArrayList over LinkedList unless you have very specific reasons for the latter (like a requirement from an assignment). For nearly all purposes ArrayList performs better. Anyway, either should work and you need not worry much about performance. Commented May 1, 2017 at 11:32

1 Answer 1

1

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.

answered May 1, 2017 at 11:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.