2017-12-27 255 views
0

分離具有一個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問題的錯誤,但請幫幫我!

+0

它可以幫助發佈(或閱讀,如果你還沒有這樣做還)錯誤。 此外,您的名單正確命名?第二次使用'SongList'和'songList'。同樣,'PathName'和'path'貌似可以互換 – lucidbrot

+0

你可以在instanceof – jeprubio

回答

2

你需要轉換的對象:

for(Object ob: myList){ 

     if(ob instanceof SongInfoModel) { 

      songList.add((SongInfoModel)ob); //<-- 
     }else if(ob instanceof String){ 

      path.add((String)ob); //<-- 
     } 
    } 
相關問題