42

Assuming that I have a class named Class,

And I would like to make a new ArrayList that it's values will be of type Class.

My question is that: How do I do that?

I can't understand from Java Api.

I tried this:

ArrayList<Class> myArray= new ArrayList ArrayList<Class>;
1
  • 7
    If you actually do have a class named Class, be aware that that could be easily confused with java.lang.Class - it won't confuse the compiler but it's likely to confuse anybody who comes along and reads your code. Commented May 6, 2011 at 19:06

9 Answers 9

59

You are looking for Java generics

List<MyClass> list = new ArrayList<MyClass>();

Here's a tutorial http://docs.oracle.com/javase/tutorial/java/generics/index.html

Sign up to request clarification or add additional context in comments.

2 Comments

That link to a tutorial PDF from Sun is broken. This series of web pages from Oracle should be helpful: docs.oracle.com/javase/tutorial/java/generics/index.html
Can also be shortened to List<MyClass> list = new ArrayList<>();, at least in Java 8.
17

If you just want a list:

ArrayList<Class> myList = new ArrayList<Class>();

If you want an arraylist of a certain length (in this case size 10):

List<Class> myList = new ArrayList<Class>(10);

If you want to program against the interfaces (better for abstractions reasons):

List<Class> myList = new ArrayList<Class>();

Programming against interfaces is considered better because it's more abstract. You can change your Arraylist with a different list implementation (like a LinkedList) and the rest of your application doesn't need any changes.

Comments

12

You're very close. Use same type on both sides, and include ().

ArrayList<Class> myArray = new ArrayList<Class>();

2 Comments

I often see just "List" on the left side (like in the accepted answer). Why do you suggest ArrayList on both sides?
@KyleClegg mellamokb just took the code line from the question. If your question was about "List vs ArrayList", you can find some answers here : stackoverflow.com/questions/2279030/…
4

You can use in Java 8

List<Class> myArray= new ArrayList<>();

1 Comment

What is the advantage of not writing Class in the <> of the right handside ?
3

Fixed the code for you:

ArrayList<Class> myArray= new ArrayList<Class>();

Comments

3

Do this: List<Class> myArray= new ArrayList<Class>();

Comments

3

Java 8

In order to create a non-empty list of fixed size where different operations like add, remove, etc won't be supported:

List<Integer> fixesSizeList= Arrays.asList(1, 2);

Non-empty mutable list:

List<Integer> mutableList = new ArrayList<>(Arrays.asList(3, 4));

Java 9

With Java 9 you can use the List.of(...) static factory method:

List<Integer> immutableList = List.of(1, 2);

List<Integer> mutableList = new ArrayList<>(List.of(3, 4));

Java 10

With Java 10 you can use the Local Variable Type Inference:

var list1 = List.of(1, 2);

var list2 = new ArrayList<>(List.of(3, 4));

var list3 = new ArrayList<String>();

Check out more ArrayList examples here.

Comments

1
    ArrayList<Class> myArray = new ArrayList<Class>();

Here ArrayList of the particular Class will be made. In general one can have any datatype like int,char, string or even an array in place of Class.

These are added to the array list using

    myArray.add();

And the values are retrieved using

    myArray.get();

Comments

0

Material please go through this Link And also try this

 ArrayList<Class> myArray= new ArrayList<Class>();

3 Comments

Never use this! It is extremely bad practice to use concrete class at the left side of assignment when interface is available.
Not sure what's wrong, this looks like all the other answers. They must have edited?
@AlexR Nothing can prevent you from using the ArrayList class on the left type if you want to use ArrayList's specific methods (trimToSize, removeRange, ...) as the List interface doesn't contain these methods.

Your Answer

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.