“Warning: [unchecked] unchecked cast” when casting Object to ArrayList

后端 未结 3 1515
轻奢々
轻奢々 2021-01-18 12:09

Strange situation - below is the code:

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

String st         


        
3条回答
  •  旧时难觅i
    2021-01-18 12:31

    The compiler complains

    ArrayList list = (ArrayList) obj[1]
    

    because a cast is a runtime check. So at runtime your ArrayList could be a ArrayList, because the type of obj is unknown.

提交回复
热议问题