0

I want to store 4 Lists of Strings to an array within index 0-3 and be able to check wether the index is filled (!=null) or not.

For that reason I need to initialize an Array of Type List, which fails in eclipse with message "Cannot create a generic array of List":

// Does not work
List<String>[] myArray = new List<String>[4];

// Does not work
List<String>[] myArray = new ArrayList<String>[4];

Doing it like promoted at Convert an ArrayList to an object array :

ArrayList<List<String>> myArrayList = new ArrayList<List<String>>();
myArrayList.add(new ArrayList<String>());
myArrayList.add(new ArrayList<String>());
myArrayList.add(new ArrayList<String>());
myArrayList.add(new ArrayList<String>());

// Does not work
List<String>[] myArray = myArrayList.toArray(new List<String>[myArrayList.size()]);

// Does not work
List<String>[] myArray = myArrayList.toArray(new ArrayList<String [myArrayList.size()]);

But why is this not working?

Community
  • 1
  • 1

1 Answers1

0

ArrayList is generic class in Java. You cannot create an array of generic type. It is not allowed. What you can do is create a list of generic type

ArrayList<ArrayList<String>> listOfStringList = new ArrayList<ArrayList<String>>();
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53