Reputation: 3728
Please suggest how to return a multiple array string in an single method:
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;
I need to return firstname
, lastname
, and email
Array string, how is it possible to return multiple array string?
Upvotes: 1
Views: 1145
Reputation: 626
Return
ArrayList< ArrayList < String>>
or create a bean containing the arrays and return it.
Upvotes: 0
Reputation: 122006
Your method signature should be change and there are two ways.
With List
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);
Then add them to main list and return.
Or even with Map ,I suggest this
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);
Then some where else, getting from this map like
List<String> emailsList = mainlist.get("email");
Upvotes: 0
Reputation: 26084
Consider this design
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;
}
or if you stick on you code then use below one.
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 ;
}
Upvotes: 0
Reputation: 111349
You can return a list of lists. For example:
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);
}
Upvotes: 2