spatara
spatara

Reputation: 901

Creating a Java List of Generic types

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?

Upvotes: 2

Views: 256

Answers (1)

arshajii
arshajii

Reputation: 129572

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>();

Upvotes: 4

Related Questions