8

In normal array list initialization, We used to define generic type as follows,

List<String> list1 = new ArrayList<String>();

But in case of ArrayList of ArrayLists, How can we define its generic type?

The code for array list of array lists is as follows:

ArrayList[] arr=new ArrayList[n];
 for(int i=0;i<n;i++)
 {
 arr[i]=new ArrayList();
 } 

Just share the syntax, if anybody have idea about it..!

3
  • 3
    this is an array of ArrayList objects Commented Feb 29, 2012 at 10:27
  • Thank you Andreas.., Until now i just thought it was an Arraylist of ArrayLists, now i got it, that it is an Array of ArrayList object..! Ok then how can we define generic type for that..? Any ideas..? Commented Feb 29, 2012 at 10:35
  • See my answer. It's not possible. No generic arrays with java. Commented Feb 29, 2012 at 10:39

5 Answers 5

13

You can simply do

List<List<String>> l = new ArrayList<List<String>>();

If you need an array of Lists, you can do

List<String>[] l = new List[n];

and safely ignore or suppress the warning.

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

3 Comments

"Safely ignore or suppress the warning." I don't think writing code that deliberately causes warnings should be considered "safe".
@TomHawtin-tackline it's quite safe. The new array contains no list objects, hence it contains no lists that are not List<String>. The compiler just isn't clever enough to recognize this. (And it isn't allowed to be more clever, by spec.) If you want an array of lists (to avoid the indirection or extra space of ArrayList, say), there is no way to do this without a warning.
You have to rely upon a line of reasoning rather than letting the compiler deal with the nonsense. / As it happens l can have a List<Integer> added, although more recent compilers will give you another ("rawtypes") warning.
5

If you (really) want a list of lists, then this is the correct declaration:

List<List<String>> listOfLists = new ArrayList<List<String>>();

We can't create generic arrays. new List<String>[0] is a compiletime error.

Comments

0

Something like this:

List<List<Number>> matrix = new ArrayList<List<Number>>();
for (int i = 0; i < numRows; ++i) {
    List<Number> row = new ArrayList<Number>();
    // add some values into the row
    matrix.add(row);
}

Make the type of the inner List anything you want; this is for illustrative purposes only.

Comments

0

You are Right: This looks insane. (May its an Bug...) Instead of Using

ArrayList<String>[] lst = new ArrayList<String>[]{};

Use:

ArrayList<String>[] list1 = new ArrayList[]{};

will work for the declaration, even if you dont describe an congrete generic!

1 Comment

why are you creating a anonymous class?
0

You are talking about an array of lists (ArrayLists to be more specific). Java doesn't allow generic array generation (except when using wildcards, see next paragraph). So you should either forget about using generics for the array, or use a list instead of an array (many solutions proposed for this).

Quote from IBM article:

Another consequence of the fact that arrays are covariant but generics are not is that you cannot instantiate an array of a generic type (new List[3] is illegal), unless the type argument is an unbounded wildcard (new List< ?>[3] is legal).

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.