2011-07-28 117 views
2

我有這個Java代碼,我想返回2個值,然後在main()或其他函數中使用它們。請幫助一些。 TX:Java - 返回2值(字符串數組)

import java.net.*; 
import java.io.*; 
import java.io.File; 
import org.apache.commons.io.FileUtils; 
import org.apache.commons.lang.StringUtils; 

public class URLReader { 

public String[] functie(String x) throws Exception 
{ 
    URL oracle = new URL(x); 
    BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); 
    String inputLine=null; 
    StringBuffer theText = new StringBuffer(); 
    while ((inputLine = in.readLine()) != null) 
      theText.append(inputLine+"\n"); 

    String html = theText.toString(); 
    in.close(); 

    String[] tds = StringUtils.substringsBetween(html, "<tr>", "</tr>"); 

    String[] tds2 = StringUtils.substringsBetween(tds[1], "href=\"/module/gallery", "\"><img"); 
    String[] tds3 = StringUtils.substringsBetween(tds[1], "src='/redx_tools/mb_image.php", "' border='1'"); 

    return ??? 

} 

public static void main(String[] args) throws Exception { 
    String x = new String("http://www.wippro.at/module/gallery/index.php?limitstart=0&picno=0&gallery_key=59"); 

    URLReader s = new URLReader(); 
    for (String st : s.functie(x)) 
    { 
     System.out.println(st); 
    } 

} 

}

回答

8

你建立你的字符串?如果ab是要返回的String對象,你可以建立一個字符串數組返回這樣的:

return new String[] {a, b}; 

你已經建立在你的代碼三個弦陣列:tdstds2tds3。他們都可以在一個大數組返回這樣的:

String[] retArray = new String[tds.length+tds2.length+tds3.length]; 
System.arrayCopy(tds, 0, retArray, 0, tds.length); 
System.arrayCopy(tds2, 0, retArray, tds.length, tds2.length); 
System.arrayCopy(tds3, 0, retArray, tds.length+tds2.length, tds3.length); 
return retArray 
0

只是2個條目返回的Vector :-)

+0

向量被認爲是不推薦的,ArrayList應該在大多數情況下使用。 –

+1

我使用Vector時,我希望它可以在所有JVM上工作,無論它們多大。只插入2個條目(甚至是2打)時,同步開銷顯然不是問題。 –

2

有你的問題很多解決方案,但我能想到的最簡單的一種是創建一個包含多個值的列表,並返回像整個列表這樣的:

公共類URLReader {

public List<String[]> functie(String x) throws Exception 
{ 
    ... 

    final String[] tds = StringUtils.substringsBetween(html, "<tr>", "</tr>"); 

    String[] tds2 = StringUtils.substringsBetween(tds[1], "href=\"/module/gallery", "\"><img"); 
    String[] tds3 = StringUtils.substringsBetween(tds[1], "src='/redx_tools/mb_image.php", "' border='1'"); 

    List<String[]> substrList = new ArrayList<String[]>(); 
    substrList.add(tds); 
    substrList.add(tds2); 
    substrList.add(tds3); 

    return substrList; 

} 

public static void main(String[] args) throws Exception { 
    String x = new String("http://www.wippro.at/module/gallery/index.php?limitstart=0&picno=0&gallery_key=59"); 

    URLReader s = new URLReader(); 
    for (String[] st : s.functie(x)) 
    { 
     System.out.println(Arrays.toString(st)); 
    } 
} 

}

+0

謝謝,但我如何使用(打印)作爲字符串數組只「tds2」? –

+0

'對於(String st:s.functie(x).get(1)) \t \t \t System.out.println(st);' –

+0

你有正確的想法,但我可以問你想達到什麼目的用這個代碼?我通常不推薦像這樣的編碼實踐(s.functie(x).get(1)) –

-1

看起來你正在做的是解析出該鏈接的圖像點,隨着一些屬性。一些觀察:

  1. 相反在一些匿名收集 返回這些值(數組,列表,等等),可以考慮構建一個類,將 握住你的價值觀。如下面的ImageProperties。這個 解決了您返回「多個」值的需求,並簡化了以後的所有編程。

  2. 正如你所寫,你正在對錶格內容和你將要處理的img標籤的特定格式進行硬編碼。如果有任何更改(如標籤的順序),則會中斷。考慮在解析中做一些更靈活的事情 。


class ImageProperties { 
    String imageHref;   // This gets what you are putting in td2 
    String imageProperties; // This gets what you are putting in td3 
    // I assume you know how to create a constructor. 
} 

然後,當然,你的 「functie」(需要一個更好的名字,當然)將返回ImageProperties的一個實例:

public ImageProperties[] functie(String url) throws Exception { 
    URL oracle = new URL(url); 
    BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); 
    String inputLine=null; 
    StringBuffer theText = new StringBuffer(); 
    while ((inputLine = in.readLine()) != null) 
      theText.append(inputLine+"\n"); 
    String html = theText.toString(); 
    in.close(); 
    // TODO: This parsing needs work to make it more change-resistant 
    String[] tds = StringUtils.substringsBetween(html, "<tr>", "</tr>"); 
    String[] tds2 = StringUtils.substringsBetween(tds[1], "href=\"/module/gallery", "\"><img"); 
    String[] tds3 = StringUtils.substringsBetween(tds[1], "src='/redx_tools/mb_image.php", "' border='1'"); 
    if (tds2.length != tds3.length) { 
     throw new ToldYouThisNeededChange(); 
    } else { 
     ImageProperties[] ret = new ImageProperties[tds2.length]; 
     for (int ii=0; ii < tds2.length; ii++) { 
      ImageProperties props = new ImageProperties(tds2[ii], tds3[ii]); 
     } 
     return ret; 
    } 
}  
+0

找不到符號 符號:構造函數ImageProperties(java.lang.String [],java.lang.String []) location:class ImageProperties 返回新的ImageProperties(tds2,tds3); ^ –

+0

functie被聲明爲返回'String []',而不是'ImageProperties'。您建議的新類的成員變量可能應該是String的數組而不是String對象,除非您認爲構造函數將聰明地將參數強制爲String對象。 (加上@博格丹說的 - 你沒有構造函數,但使用一個) – Atreys

+0

@Atreys。謝謝,我錯過了返回類型的變化。不同意字符串數組 - 原始的「functie」只能找到其中的一個。至於構造函數,我也跳過了一個toString方法和hashcode和equals。不過,再次感謝回報類型的捕獲。 – CPerkins

2

伊夫問自己這個問題過去幾次。我最近的做法是返回一個Map。它是返回多個值的最靈活的方式。假設你需要返回一個String,一個int,甚至一個類。使用Map(不使用泛型)可以返回所有這些。我認爲這比聲明只能作爲數據容器的內部類更好。

+0

這是個好主意。閱讀代碼的人知道到底發生了什麼,因爲顯式投射(如果需要的話)。 – Atreys