What does this mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does this mean?

List<Integer>[ ] a = new ArrayList[20] ;

14th Sep 2020, 7:05 PM
R_N
7 Answers
+ 2
List<Integer> a = new ArrayList<>(); The class ArrayList implements the List interface. Here it simply means that you have created an ArrayList which can only store Integer objects.
14th Sep 2020, 7:15 PM
Avinesh
Avinesh - avatar
+ 1
I mean this..... List<Integer> [ ] a = new ArrayList[20] ; Here, a[0].add(20); a[0].add(30); add 20 and 30 to the first index of array. I saw this somewhere but unable to understand.
14th Sep 2020, 7:29 PM
R_N
0
It is nothing but an array of ArrayList just like any other array. Each index value is a dynamic array.
14th Sep 2020, 7:35 PM
Avinesh
Avinesh - avatar
0
But why does this give unchecked warning?
14th Sep 2020, 8:04 PM
R_N
0
It is throwing a NullPointerException and it is probably due to the fact that arrays are meant to be of fixed size but since you have ArrayList's inside the array which are dynamic and can be resized, you get the following exception.
14th Sep 2020, 8:23 PM
Avinesh
Avinesh - avatar
0
Unchecked warning appears when you are using collections without specifying the type and compiler cannot check if you are using collection in a type-safe way. So, if you are declaring list like List list instead of List<String> list, compiler won't be able to prevent errors this might cause. In your example message appears because we can't create arrays with parameterized types in Java. Below is a code with wrong usage of list, which causes warning to appear. https://code.sololearn.com/c2HdapIakVS2/?ref=app
14th Sep 2020, 9:48 PM
Aleksandrs
Aleksandrs - avatar
0
Avinesh ,Aleksandrs Kalinins Thanks for the answers.
15th Sep 2020, 3:18 AM
R_N