2016-11-18 35 views
0

美好的一天!在其他類中使用數組輸出

我有我返回報表名稱

System.out.println(bc[i].getDefaultName().getValue() 

我想使用陣列輸出在其他類的陣列的方法,我需要怎麼聯繫方法outpud在我在其他類數組?

方法是:

public class ReoprtSearch { 
    public void executeTasks() { 
     PropEnum props[] = new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName}; 
     BaseClass bc[] = null; 
     String searchPath = "//report"; 
    //searchPath for folder - //folder, report - //report, folder and report - //folder | //report 

     try { 
      SearchPathMultipleObject spMulti = new SearchPathMultipleObject(searchPath); 
      bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return; 
     } 

     if (bc != null) { 
      for (int i = 0; i < bc.length; i++) { 

       System.out.println(bc[i].getDefaultName().getValue(); 
      } 
     } 
    } 
} 

陣列我想要把數組的樣子:

String [] folders = 

我想這樣的:

ReoprtSearch search = new ReoprtSearch();  
String [] folders = {search.executeTasks()}; 

返回我一個錯誤:無法轉換從無效到字符串

給我一個解釋,以瞭解如何我可以從其他類的方法輸出相關。

感謝

回答

1

的問題是,你的executeTasks方法實際上並不返回任何東西(這就是爲什麼它是void),只是打印到標準輸出。而不是打印,將名稱添加到數組,然後將其返回。像這樣的:

public class ReoprtSearch { 
    public String[] executeTasks() { 
     PropEnum props[] = new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName}; 
     BaseClass bc[] = null; 

     String searchPath = "//report"; 
    //searchPath for folder - //folder, report - //report, folder and report - //folder | //report 

     try { 
      SearchPathMultipleObject spMulti = new SearchPathMultipleObject(searchPath); 
      bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 

     if (bc != null) { 
      String results[] = new String[bc.length]; 
      for (int i = 0; i < bc.length; i++) { 
       results[i] = bc[i].getDefaultName().getValue(); 
      } 
      return results; 
     } 
     return null; 
    } 
} 
+0

我不明白你的意見。我誤解了你的問題嗎? –

+0

起初我不明白的意思。你展示瞭如何將輸出寫入數組。我會嘗試使用它來測試並找出 –

+0

Alejandro,爲什麼在Eclipse 中爲''public String [] executeTasks()'行說我:'這個方法必須返回一個String []類型的結果' '結果;'最後? 我們使用'return results;'返回'string []'',因爲您將結果聲明爲字符串行'String results [] = new String [bc.length];' –

相關問題