List myList = new ArrayList();
or with generics (Java 7 or later)
List<MyType> myList = new ArrayList<>();
or with generics (Old java versions)
List<MyType> myList = new ArrayList<MyType>();
Answer from Dan Vinton on Stack OverflowList myList = new ArrayList();
or with generics (Java 7 or later)
List<MyType> myList = new ArrayList<>();
or with generics (Old java versions)
List<MyType> myList = new ArrayList<MyType>();
Additionally, if you want to create a list that has things in it (though it will be fixed size):
List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
How do I make integer lists in Java?
How to create a List in Java - Stack Overflow
Multiple ways to declare a list(JAVA)?
What is the best way to add items to a list?
It seems a lot easier from the very little time I spent on Python before making the jump to Java. I'm taking a college night class on Java, and lists are one thing that are confusing me. How do they work in Java?
There are three immediate issues here:
Type parameters cannot be primitive; you need to use the appropriate wrapper class. In your case, it would be
List<Integer>.Listis an interface: you cannot instantiate it. You need to instantiate a class that implements it, a common option isArrayList:List<Integer> myList = new ArrayList<Integer>();In Java 7 and above you don't actually have to restate the type parameter on the right hand side of the assignment:
List<Integer> myList = new ArrayList<>();List/ArrayListare not in thejava.langpackage, so you have to import them by including the appropriateimportstatements at the top of your file (under the package declaration, if there is any):import java.util.ArrayList; import java.util.List;That's why it's complaining that the "symbol cannot be found".
First of all, int cannot be used in paramertized types. You can use Integer instead.
Secondly, List is an interface, you cannot instantiate a List. You can use the ArrayList class which implements List:
ArrayList<Integer> list = new ArrayList<Integer>();
Or:
List<Integer> list = new ArrayList<Integer>();