数组字符串方法返回多字符串方法

时间:2013-10-25 13:33:50

标签: java arrays selenium selenium-webdriver

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

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;

我需要返回firstnamelastnameemail数组字符串,如何返回多个数组字符串?

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);

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

或者即使使用地图,我建议

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

答案 2 :(得分:0)

返回

ArrayList< ArrayList < String>>

或创建一个包含数组的bean并将其返回。

答案 3 :(得分: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 ;
}