There are three same type lists.

ArrayList<AAA> a0= new ArrayList<AAA>(); ArrayList<AAA> a1= new ArrayList<AAA>(); ArrayList<AAA> a2= new ArrayList<AAA>(); 

where AAA is my class.

There is a variable int Cnt; which takes the value 0, 1, or 2. Depending on this variable, you need to use the appropriate list. It is logical to make an array of these lists, but I don’t understand how. There must be something like this:

 ArrayList<AAA> a[]= new ArrayList<AAA>[]{ new ArrayList<AAA>(), new ArrayList<AAA>(), new ArrayList<AAA>()}; 

But the compiler swears and I won’t figure out how to do something.

  • 1) And here is the AAA class? 2) How are you going to use the appropriate list, depending on the value of the variable? 3) Why is this logical? - Android Android
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

In general, no way: arrays in java are covariant by element type, but they must check the type of the assigned value of the element at run time, ie:

 Object[] array = new String[10]; // массив строк является массивом объектов array[0] = new Object(); 

the compiler will skip, but java.lang.ArrayStoreException will java.lang.ArrayStoreException on the second line at runtime. Generics in Java are implemented by erasing information about the type parameter, so check that the assigned ArrayList is of type ArrayList<AAA> at run time cannot, and the creation of arrays with this type of element is prohibited.

It is usually recommended not to contact, and instead of arrays to use lists. If you really, really want to have an array, you can declare an array of lists without specifying the type:

 ArrayList[] a = new ArrayList[] { new ArrayList<AAA>(), new ArrayList<AAA>(), new ArrayList<AAA>()}; a[1] = new ArrayList<AAA>(); AAA value = ((ArrayList<AAA>)a[1]).get( 0 ); AAA otherValue = (AAA)a[1].get( 0 ); // ммм... как в 1.4.2 

and lead when used to a parameterized type, but the compiler will give warnings about unchecked cast.

You can also create your own type, which expands the parameterized type with the desired type of parameter.

 static class AAAList extends ArrayList<AAA> {} 

and use it

 AAAList[] a = new AAAList[] { new AAAList(), new AAAList(), new AAAList()}; a[1] = new AAAList(); AAA value = a[1].get( 0 ); ArrayList<AAA> list = a[1]; 

although this is unlikely to help you.

  • That's just "a type that expands a parameterized type with the desired type of parameter" is very similar to what I need, I will try now. - Alexander Bezfamilnyy
  • Thank you very much, everything turned out just what I need! - Alexander Bezfamilnyy

Here is an example of what you want:

 ArrayList<ArrayList> list = new ArrayList<ArrayList>(); ArrayList<Integer> subList = new ArrayList<Integer>(); list.add(subList); list.get(0); // доступ к subList 
  • I need access to the desired list not through switch / case, but through the array index. Something like this: a [Cnt] .add (..); - Alexander Bezfamilnyy
  • @ Alexander Bezfamilny, and you can tell why you need it? What exactly will your program and this code do? - Arsenicum
  • It is necessary to optimize the code. The task is classic, there is a list of orders for today, there are planned orders for today, there are orders for tomorrow. Working with them is almost the same, they are switched by pressing one of the three buttons. And I want the button to change only Cnt and send a message to update the screen. Plus, in a separate stream, I receive orders from the server and I need to scatter them in the necessary lists. - Alexander Bezfamilnyy
  • @Alexander Bezfamilny, I don’t undertake to evaluate the correctness of building your application, but I added the answer with the code you need. - Arsenicum
  • No, the list of lists does not want to, here in the second answer is something similar to my desires, I will go to understand and try. - Alexander Bezfamilnyy