我如何字符串值和对象类型值从对象数组列表
问题描述:
分离具有一个ArrayList我如何字符串值和对象类型值从对象数组列表
private ArrayList<Object> myList = new Arraylist<>()
此ArrayList包含字符串值和SongInfoModel(对象)的值。
如何将这两个检索到它们各自的数组列表?
private ArrayList<SongInfoModel> songList = new Arraylist<>()
private ArrayList<String> path= new Arraylist<>()
我使用如下代码,但它给我的 “添加” 声明
for(Object ob: myList){
if(ob instanceof SongInfoModel) {
songList.add(ob); //error
}else if(ob instanceof String){
path.add(ob); //error
}
}
我知道这是一个noobish问题的错误,但请帮帮我!
答
你需要转换的对象:
for(Object ob: myList){
if(ob instanceof SongInfoModel) {
songList.add((SongInfoModel)ob); //<--
}else if(ob instanceof String){
path.add((String)ob); //<--
}
}
它可以帮助发布(或阅读,如果你还没有这样做还)错误。 此外,您的名单正确命名?第二次使用'SongList'和'songList'。同样,'PathName'和'path'貌似可以互换 – lucidbrot
你可以在instanceof – jeprubio