2

I have created a Java class MyObject:

public class MyObject
{
    public final String s; 
    public final int i; 
    public final int j;
    ...
}

I want to create a List of this type, but I am getting a "Cannot instantiate the type List" error

private java.util.List<MyObject> myObjectss = new java.util.List<MyObject>();

I tried the solution in this question (4 vote answer) but I get a "The type List is not generic; it cannot be parameterized with arguments " error...

Java Linked List How to create a node that holds a string and an int?

1 Answer 1

4

List is an interface and therefore cannot be instantiated. You could try:

private java.util.List<MyObject> myObjectss = new java.util.ArrayList<MyObject>();

ArrayList is a class that implements the List interface.


Edit Just saw that you needed a LinkedList, try this:

private java.util.List<MyObject> myObjectss = new java.util.LinkedList<MyObject>();
Sign up to request clarification or add additional context in comments.

4 Comments

OP needs LinkedList. It's pretty similar but still the answer could have better guidance.
ah, I see now. I thought I might have been using List wrong...but not how. Thanks!
Also, you can instantiate an interface in your code by creating an anonymous class, but it's clumsy to use it for the List interface.
Yes you can do this also. I don't remember if this would be called "instantiating List" since you are technically implementing it in an anonymous class and then instantiating that class. But anyway, that's a technicality.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.