开发者

Java - return 2 values (String array)

开发者 https://www.devze.com 2023-03-23 20:28 出处:网络
I have this java code and i want to return 2 values and then use them in main() or in other functions. Some help please. TX :

I have this java code and i want to return 2 values and then use them in main() or in other functions. Some help please. TX :

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开发者_C百科 {

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);
    }

}

}


Did you build your strings? if a and b are the String objects you want to return, you can build a String array to return like this:

return new String[] {a, b};

You've built three String arrays in your code: tds, tds2, and tds3. All of them could be returned in a big array like this:

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


There are many solutions to your question, however the simplest one I can think of is to create a list containing multiple values and return the entire list like this:

public class 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));
    }
}

}


Ive asked myself that question several times in the past. My most recent way of doing this is to return a Map. Its the most flexible way of returning several values. Let's say you need to return a String, an int and maybe even a class. With a Map (not using generics) you can return all these. I think this is better than declaring an inner class that would only serve as a data container.


Just return a Vector with 2 entries :-)


Looks like what you're doing is parsing out the link an image points to, along with some properties. A couple of observations:

  1. Instead of returning these values in some anonymous collection (array, list, whatever), consider constructing a class which will hold your values. Something like ImageProperties below. This solves your need to return "multiple" values, and simplifies all your later programming.

  2. As written, you're hardcoding a dependency on the specific format of the table contents and the img tags you will be processing. If anything changes (like the order of tags), you'll break. Consider doing something a bit more flexible in your parsing.


 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.
 }

Then, of course, your "functie" (needs a better name, of course) would return an instance of 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

精彩评论

暂无评论...
验证码 换一张
取 消