31

I want to create an array that contains ArrayList<String> elements.

I've tried

ArrayList<String> name[] = new ArrayList<String>()[];

but that doesn't seem to work.

3
  • You want an arraylist of arraylists. Why would you use an array in one sense and an arraylist in others?
    – Falmarri
    Dec 28, 2010 at 20:25
  • 2
    I know how many elements there will be. Is it still better to use an arraylist of arraylists?
    – sighol
    Dec 28, 2010 at 20:27
  • 1
    This is a duplicate of: stackoverflow.com/questions/2792731/…
    – Tom
    Dec 29, 2010 at 4:07

12 Answers 12

38

You cannot create an array of a generic type.

Instead, you can create an ArrayList<ArrayList<String>>.

1
12

The correct way is:

ArrayList<String> name[] = new ArrayList[9];

However, this won't work either, since you can't make an array with a generic type, what you are trying to do is a matrix, and this should be done like this:

String name[][];
1
  • 5
    but the first DOES work, however it shows a type safety warning.
    – user85421
    Dec 28, 2010 at 21:11
11

I know this is a bit old but I am going to respond to this anyway for future views.

If you really want an ArrayList<String>[] structure, you can simply create a class that extends ArrayList and make an array of that class:

public class StringArrayList extends ArrayList<String>{}

And in your implementation:

ArrayList<String> name[] = new StringArrayList[9];

Here is a sample:

package testspace.arrays;

import java.util.List;

public class TestStringArray {

    public static void main(String[] args) {
        List<String>[] arr = new StringArrayList[10];
        for(int i = 0; i < arr.length; i++){
            // CANNOT use generic 'new ArrayList<String>()'
            arr[i] = new StringArrayList(); 
            for(int j = 0; j < arr.length; j++){
                arr[i].add("list item #(" + j + "|" + i + ")");
            }
        }

        StringBuilder sb = new StringBuilder();
        for(final List<String> list : arr){
            for(final String str : list){
                sb.append(str + " ");
            }
            sb.append("\n");
        }
        System.out.println(sb.toString());
    }

}

NOTE You will get a runtime error if you use this instead : arr[i] = new ArrayList<String>()

5

This is not proper OO and this sort of code is very implementation coupled. If you need to do such thing, probably you did something wrong. If the code is not yours, the person who made it probably did something wrong.

If you know how many elements are there (or even if you didn't), why not use Map<Integer,List<String>>?

0
2
ArrayList<ArrayList<String>> name= new ArrayList<ArrayList<String>>(/*capacity*/);
2

If you do want an array of ArrayList, you'll have to initialise each position of the array individually:

int size = 9; // 9 is just an example
// you can remove the annotation, but you'll be warned:
// Type safety: The expression of type ArrayList[] needs unchecked conversion 
// to conform to ArrayList<String>[]
@SuppressWarnings("unchecked")
ArrayList<String> name[] = new ArrayList[ size];
for( int i = 0; i < size; i++) {
    name[ i] = new ArrayList<String>();
}
0

I have tried this without an error, even though I have putted a limit in the array less than the amount of items added:

package Testes.src;

import java.util.ArrayList;
import java.util.List;

public class Testes {

    private List<String> name=new ArrayList<String>(1);

    public Testes(){
        for (int i = 1; i <= 100; i++) {
            name.add("Teste"+Integer.toString(i));
        }
        for (int i = 0; i < name.size(); i++) {
            System.out.println(name.get(i));
        }
    }
    public static void main(String [] args)
    {
        Testes t1=new Testes();
    }
}
0

I know this is an old post, but someone just asked the same question and since nobody has mentioned the following solution, here it is.

One way to get around it could be something like a 2d array (the recent question was about a 2d array or ArrayLists) of objects and then you should be able to put ArrayList references in the array. I have not tested it, but something like this should work.

Object [][] data = new Object[5][5];
data[0][0] = new ArrayList<String>(); // etc . . .
0

You may try this:

ArrayList[] arr=new ArrayList[5];
for(int i=0;i<5;i++)
     arr[i]=new ArrayList<String>();
arr[1].add("35");
arr[1].add("ME");
arr[1].add("HELLO");
System.out.println(arr[1]);

Output:

[35, ME, HELLO]
0
// This is the not correct way
ArrayList<String> name[] = new ArrayList<String>()[];

// This is the correct way
ArrayList<String> list[] = new ArrayList[2];

list[0] = new ArrayList();
list[1] = new ArrayList();

list[0].add("Some String 11");
list[0].add("Some String 22");

list[1].add("Some String 1111");
list[1].add("Some String 2222");
1
  • 2
    This was answered 7 years ago. What new value does your answer bring? Oct 8, 2017 at 22:05
0

Array of ArrayLists, example with Loops

//Declare
Object[] arrayLists = new Object[size];    

//Fill
for (int j=0; j < size; j++) {
     arrayLists[k]= generateAnArrayList(j);  //or whatnot
}

//Usage
for (int j=0; j < size; j++) {
    ArrayList<Long> al = (ArrayList<Long>) arrayLists[j];
    //do something here
}
0

There is a way to do it with only little modification of your code.

ArrayList<String>[] name = new ArrayList[10]; 

This will give you a array of ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.