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

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

}

}

最佳答案

你的琴弦是自己制作的吗?如果ab是你想要返回的String对象,你可以构建一个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

关于Java - 返回 2 个值(字符串数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6864640/

相关文章:

c - 如何为 const char* 数组赋值并打印到屏幕

java - 字符串不变性

java - 显示 bash :/usr/lib/jvm/java-7-oracle=/usr/lib/jvm/java-8-oracle: No such file or directory in terminal

javascript - .click 函数在没有参数的情况下工作,但不使用参数

java - 我应该使用 static 还是 getters/setters?

iphone - 如何在 iPhone 上连接两个字符串?

string - 在 PHPStorm 中的代码完成期间跳转到字符串的结束引号

java - 存储和获取对象列表的有效方法

java - 带有 return 语句的奇怪警告

c# - List <double [] []>到List <double []>