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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sighol
  • 2,678
  • 6
  • 28
  • 34
  • You want an arraylist of arraylists. Why would you use an array in one sense and an arraylist in others? – Falmarri Dec 28 '10 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 '10 at 20:27
  • 1
    This is a duplicate of: http://stackoverflow.com/questions/2792731/how-to-do-an-array-of-hashmaps/2792743#2792743 – Tom Dec 29 '10 at 04:07

12 Answers12

38

You cannot create an array of a generic type.

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Hi, in that case, how was it possible in here? https://stackoverflow.com/questions/22747528/how-to-create-an-array-of-arraylists-in-java – Harsha Jul 23 '17 at 03:06
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[][];
Carlos Valenzuela
  • 816
  • 1
  • 7
  • 19
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>()

Glo
  • 161
  • 1
  • 6
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>>?

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>();
}
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
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();
    }
}
Roberto Santos
  • 606
  • 1
  • 7
  • 11
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 . . .
Sanjeev
  • 1,517
  • 1
  • 18
  • 30
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]
Sarthak Manna
  • 59
  • 2
  • 5
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");
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
}
BAMF4bacon
  • 523
  • 1
  • 7
  • 21
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.