1

请建议如何在单个方法中返回多个数组字符串:

public ArrayList<String> scraptable() throws InterruptedException {
    pui = new Polled(driver);
    ArrayList<String> firstname = new ArrayList<String>();
    ArrayList<String> lastname = new ArrayList<String>();
    ArrayList<String> email = new ArrayList<String>();

    WebElement usertable = pui.tbl_userlist;
    List<WebElement> allRows = usertable.findElements(By.tagName("tr"));

    for (int row = 1; row < allRows.size(); row++) {
        String Firstname = driver.findElement(
                By.xpath("//*[@id='tbUserList']/tbody/tr[" + row
                        + "]/td[2]")).getText();
        firstname.add(Firstname);

        String Lastname = driver.findElement(
                By.xpath("//*[@id='tbUserList']/tbody/tr[" + row
                        + "]/td[3]")).getText();
        lastname.add(Lastname);

        String Email = driver.findElement(
                By.xpath("//*[@id='tbUserList']/tbody/tr[" + row
                        + "]/td[4]")).getText();
        email.add(Email);           
    }
    return null;

我需要返回firstname、、lastnameemail数组字符串,怎么可能返回多个数组字符串?

4

4 回答 4

2

您可以返回列表列表。例如:

import java.util.Arrays; // needed to access "Arrays" class with short name

public List<ArrayList<String>> scraptable() throws InterruptedException {
    ...
    return Arrays.asList(firstname, lastname, email);
}
于 2013-10-25T13:37:29.520 回答
0

你的方法签名应该改变,有两种方法。

清单

public List<List<String>> scraptable() throws InterruptedException {
   List<List<String>> mainlist = new ArrayList<List<String>>();
   ArrayList<String> firstname = new ArrayList<String>();
   ArrayList<String> lastname = new ArrayList<String>();
   ArrayList<String> email = new ArrayList<String>();
    ---
    ---
    --
  mainlist.add(email);
  mainlist.add(lastname);
  mainlist.add(firstname);

然后将它们添加到主列表并返回。

甚至使用Map,我建议这样做

public  Map<String, List<String>> scraptable() throws InterruptedException {

  Map<String, List<String>> mainlist = new HashMap<String, List<String>>();
      ArrayList<String> firstname = new ArrayList<String>();
      ArrayList<String> lastname = new ArrayList<String>();
      ArrayList<String> email = new ArrayList<String>();

      mainlist.put("email",email);
      mainlist.put("lastname",lastname);
      mainlist.put("firstname",firstname);

然后在其他地方,从这张地图中获取

 List<String> emailsList = mainlist.get("email");
于 2013-10-25T13:35:15.547 回答
0

返回

ArrayList< ArrayList < String>>

或者创建一个包含数组的 bean 并返回它。

于 2013-10-25T13:35:49.847 回答
0

考虑这个设计

class SampleObject{
    String firstname;
    String lastname;
    String email;
    //getter and setter;
}

public List<ArrayList<String>> scraptable() throws InterruptedException {
    ArrayList<SampleObject> listofObjects= new ArrayList<SampleObject>();

     for (int row = 1; row < allRows.size(); row++) {
         SampleObject s= new SampleObject();

         listofObjects.add(s);
     }
    return listofObjects;
}

或者,如果您坚持使用代码,则使用以下代码。

public List<ArrayList<String>> scraptable() throws InterruptedException {
    List<List<String>>  listOfList =new ArrayList<List<String>> ();

   listOfList.add(firstname); 
   listOfList.add(lastname);
   listOfList.add(email);
    return listOfList ;
}
于 2013-10-25T13:46:27.337 回答