0

Like the title says, I am trying to instantiate an ArrayList with type of a class that I want to be an array. Probably I did not explain it correctly in technical terms, so let's just look at a pseudo code:

List<TestClass[5]> lTestList = new ArrayList<TestClass[5]>();

Where should I specify the size of the TestClass type array? Apologies if my explanation does not make much sense as I am still learning.

1
  • This is a completely unrelated statement, but Java has a restriction that you cannot create an array of generic objects. For example, you can't make an array of List<TestClass> like List<TestClass>[]. Of course, you can make a List of non-generic arrays, as such, List<TestClass[]>. I just figured it's interesting to know in case you didn't. Commented Oct 11, 2014 at 12:31

2 Answers 2

2

The type of a TestClass array is TestClass[]. So you need to use that as your generic parameter.

List<TestClass[]> lTestList = new ArrayList<TestClass[]>();

Then when you add items to your list, you can add arrays of the appropriate size.

lTestList.add(new TestClass[5]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I feel a bit embarassed now.
1
List<TestClass[]> lTestList = new ArrayList<TestClass[]>();

You shouldn't specify size of array in generic.

Comments

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.