2012-06-02 215 views
1

ArrayList中我有一個方法,有兩個的ArrayList返回一個對象:獲取從對象

return new Object[] {work, play}; 

我試圖讓他們在另一種方法退了出去。我已經嘗試投射到ArrayList,但我得到錯誤'數組需要,但發現java.lang.Object'。

ArrayList setWork = (ArrayList)obj[0]; 
ArrayList setPlay = (ArrayList)obj[1]; 

爲ArrayList的創作全碼:

public static Object[] getWorkandPlay(ArrayList al) { 

    ArrayList work = new ArrayList(); 
    ArrayList play = new ArrayList(); 

    for (int i=0; i<al.size(); i++){ 
     String item = (String) al.get(i); 

     if (item.startsWith("w.")) { 
      System.out.println("w " + item); 
      work.add(item); 
     } else if (item.startsWith("p.")) { 
      System.out.println("p " + item); 
      play.add(item); 
     } else { 
      System.out.println("Entries must start with either w. or p.\n"); 
     } 
    } 
    return new Object[] {work, play}; 
} 
+0

你爲什麼要返回'ArrayList'內的一個'對象'數組? –

+0

你可以通過聲明並創建工作和播放的代碼嗎? –

+0

從錯誤,它看起來像obj是一個對象,而不是對象數組。 – RedGreasel

回答

1

我做這樣的事情根據您的代碼和它的作品...

class Test{ 
    public static Object[] getWorkandPlay(ArrayList al) { 
     ArrayList work = new ArrayList(); 
     ArrayList play = new ArrayList(); 

     for (int i=0; i<al.size(); i++){ 
      String item = (String) al.get(i); 

      if (item.startsWith("w.")) { 
       System.out.println("w " + item); 
       work.add(item); 
      } else if (item.startsWith("p.")) { 
       System.out.println("p " + item); 
       play.add(item); 
      } else { 
       System.out.println("Entries must start with either w. or p.\n"); 
      } 
     } 
     return new Object[] {work, play}; 
    } 
    public static void main(String[] args) { 
     ArrayList<String> al=new ArrayList<>(); 
     al.add("w. test"); 
     al.add("p. test"); 
     Object[] obj=getWorkandPlay(al); 
     ArrayList setWork = (ArrayList)obj[0]; 
     ArrayList setPlay = (ArrayList)obj[1]; 
    } 
} 

輸出

w w. test 
p p. test 
+0

我將此應用於我的代碼考慮了其他方法,它的工作原理。謝謝。 –

1

返回新對象[] {工作,娛樂};我想返回一個對象數組。嘗試ArrayList result = new ArrayList(); 把工作和玩耍放在結果裏然後返回結果。然後

1

在您的調用代碼中,應該將obj的引用類型設置爲Object數組。 您的代碼中可能有 Object obj = getWorkandPlay(anArrayList);。將其更改爲Object[] obj = getWorkandPlay(anArrayList);

1

你可能有一個錯字某處在你的代碼。這爲我編譯:

import java.util.*; 

public class SampleClass { 

    public static void main(String[] args) { 
     // Create an ArrayList and add some sample Strings 
     ArrayList al = new ArrayList(); 
     al.add("w. test"); 
     al.add("p. test"); 

     Object[] obj = getWorkandPlay(al); 

     ArrayList setWork = (ArrayList)obj[0]; 
     ArrayList setPlay = (ArrayList)obj[1]; 
    } 

    public static Object[] getWorkandPlay(ArrayList al) { 
     ArrayList work = new ArrayList(); 
     ArrayList play = new ArrayList(); 

     for (int i=0; i<al.size(); i++){ 
      String item = (String) al.get(i); 

      if (item.startsWith("w.")) { 
       System.out.println("w " + item); 
       work.add(item); 
      } else if (item.startsWith("p.")) { 
       System.out.println("p " + item); 
       play.add(item); 
      } else { 
       System.out.println("Entries must start with either w. or p.\n"); 
      } 
     } 

     return new Object[] {work, play}; 
    } 
}