ArrayList<String>[] lists = {new ArrayList<String>(), new ArrayList<String>()}; 

Why does not it work?

There is no need to offer options using ArrayList, but ArrayLists are not needed, I just want to know what the problem is, googled about arrays and generics on the Internet, did not find anything suitable.

    1 answer 1

    Because it is impossible. Oracle Documentation :

    "You can't create arrays of parameterized types"

    But, in principle, you can cheat:

     class MyStringArrayList extends ArrayList<String> { } 

    And then:

     MyStringArrayList[] lists = {new MyStringArrayList(), new MyStringArrayList()}; 
    • Thank you, then this question: / * Array of lists of strings Create an array whose elements are lists of strings. Fill an array with any data and display it on the screen. * / here is the task, what the hell is that, why do they set such a task, maybe there is some alternative variant of initializing the array with the sheets? - Maxgmer
    • Maybe in the task under the list of strings it means not ArrayList<String> ? - s8am
    • and what can be meant by the list of strings yet? - Maxgmer
    • @Maxgmer there are not only one-dimensional arrays + what prevents you from creating a class that describes the necessary data and creating an array of objects of this class? - Nikita Kragel
    • @Maxgmer, It is necessary to ask the one who gave the task. Look, I updated the post there. - s8am