Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Warning: [unchecked] unchecked cast" when casting Object to ArrayList<String[]>

Strange situation - below is the code:

ArrayList<String[]> listArr = new ArrayList<>();
Object[] obj = new Object[]{"str", listArr};

String str = (String) obj[0];//OK
ArrayList<String[]> list = (ArrayList<String[]>) obj[1];//warning: [unchecked] unchecked cast

When project is built (with compiler option -Xlint:unchecked in project properties), I get one warning:

warning: [unchecked] unchecked cast
ArrayList list = (ArrayList) obj[1];
required: ArrayList
found: Object

But casting String in the same way is OK. What is the problem here?

like image 234
Ernestas Gruodis Avatar asked Mar 05 '15 08:03

Ernestas Gruodis


1 Answers

This is because the compiler can not verify the internal types at the list level, so you need to first verify for list. And the internal types individually.

Instead of ArrayList<String[]> list = (ArrayList<String[]>) obj[1];

It should be ArrayList<?> list = (ArrayList<?>) obj[1];

like image 180
shikjohari Avatar answered Sep 28 '22 18:09

shikjohari