Java-返回2个值(字符串数组)

Java-返回2个值(字符串数组),java,arrays,string,Java,Arrays,String,我有这个java代码,我想返回2个值,然后在main()或其他函数中使用它们。请帮忙。德克萨斯州: 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

我有这个java代码,我想返回2个值,然后在main()或其他函数中使用它们。请帮忙。德克萨斯州:

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

}
import java.net.*;
导入java.io.*;
导入java.io.File;
导入org.apache.commons.io.FileUtils;
导入org.apache.commons.lang.StringUtils;
公共类URL阅读器{
公共字符串[]函数(字符串x)引发异常
{
URL oracle=新URL(x);
BufferedReader in=新的BufferedReader(新的InputStreamReader(oracle.openStream());
字符串inputLine=null;
StringBuffer theText=新的StringBuffer();
而((inputLine=in.readLine())!=null)
追加(inputLine+“\n”);
字符串html=theText.toString();
in.close();
String[]tds=StringUtils.substringsBetween(html,“,”);
String[]tds2=StringUtils.substringsBetween(tds[1],“href=\”/module/gallery“,“\”>

}

只需返回一个包含2个条目的向量:-)

是否构建了字符串?如果
a
b
是要返回的字符串对象,则可以构建一个字符串数组,如下所示返回:

return new String[] {a, b};
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
您已经在代码中构建了三个字符串数组:
tds
tds2
tds3
。所有这些字符串都可以像这样以一个大数组返回:

return new String[] {a, b};
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

您的问题有很多解决方案,但我能想到的最简单的解决方案是创建一个包含多个值的列表,并按如下方式返回整个列表:

return new String[] {a, b};
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
公共类URL阅读器{

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));
    }
}
public List函数(字符串x)引发异常
{
...
最终字符串[]tds=StringUtils.substringsBetween(html,“,”);
String[]tds2=StringUtils.substringsBetween(tds[1],“href=\”/module/gallery“,“\”>substrList=new ArrayList();
子列表添加(tds);
子列表添加(tds2);
子列表添加(tds3);
返回子列表;
}
公共静态void main(字符串[]args)引发异常{
字符串x=新字符串(“http://www.wippro.at/module/gallery/index.php?limitstart=0&picno=0&gallery_key=59");
URLReader s=新的URLReader();
对于(字符串[]st:s.functie(x))
{
System.out.println(Arrays.toString(st));
}
}

}

看起来您所做的是解析图像指向的链接以及一些属性。下面是几个观察结果:

  • 而不是在某个匿名集合中返回这些值 (数组,列表,无论什么),考虑构造一个类 保留您的值。类似下面的
    ImageProperties
    。此 解决了返回“多个”值的需要,并简化了所有 您以后的编程

  • 如前所述,您正在硬编码对特定格式的依赖项 您将要处理的表内容和img标记。如果有 改变(如标签的顺序),你会打破。考虑做某事。 在解析过程中要灵活一点


  • 当然,您的“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;
         }
     }       
    
    public ImageProperties[]函数(字符串url)引发异常{
    URL oracle=新URL(URL);
    BufferedReader in=新的BufferedReader(新的InputStreamReader(oracle.openStream());
    字符串inputLine=null;
    StringBuffer theText=新的StringBuffer();
    而((inputLine=in.readLine())!=null)
    追加(inputLine+“\n”);
    字符串html=theText.toString();
    in.close();
    //TODO:此解析需要进行一些工作,以使其更能抵抗更改
    String[]tds=StringUtils.substringsBetween(html,“,”);
    
    String[]tds2=StringUtils.substringsBetween(tds[1],“href=\”/module/gallery“,“\”>我过去曾多次问过自己这个问题。我最近的方法是返回一个映射。这是返回多个值的最灵活的方法。假设你需要返回一个字符串,一个int,甚至一个类。使用映射(不使用泛型)您可以返回所有这些。我认为这比声明一个仅用作数据容器的内部类要好。

    谢谢,但是我如何将(print)用作仅限字符串数组的“tds2”?
    for(string st:s.functie(x.get(1))System.out.println(st)
    你的想法是对的,但是我可以问一下你想用这段代码实现什么吗?我通常不推荐这样的编码实践(s.functie(x).get(1)),我想获得一些图像和描述。找不到符号:构造函数ImageProperties(java.lang.String[],java.lang.String[])位置:类ImageProperties返回新的ImageProperties(tds2,tds3);^functie声明为返回
    字符串[]
    ,而不是
    ImageProperties
    。您建议的新类的成员变量可能应该是字符串数组,而不是字符串对象,除非您认为构造函数会做一些聪明的事情,将参数强制为字符串对象。(加上@Bogdan所说的——您没有构造函数,只能使用一个)@Atrys。谢谢,我错过了对返回类型的更改。对字符串数组的看法不一致-原始的“functie”只能找到每个字符串中的一个。至于构造函数,我还跳过了toString方法和hashcode以及equals。不过,再次感谢您对返回类型的捕获。@CPerkins,OP中的原始函数在结束了。问题不太清楚它应该做什么。从您的代码中可以明显看出tds2和tds3是一个数组。当它们被提供给构造函数时,它们似乎与类存储的数据有关。混淆加深了。是否假设tds2和3数组每个项目只有一个?很好。我查看了它们,误读了
    substringsBetween
    ,因为
    substringbeween
    。将重写。向量被认为是不推荐使用的,大多数情况下应该使用ArrayList。当我想让它在所有JVM上工作时,不管它们有多旧,我都会使用向量。同步开销显然是没有问题的