I wan to use multi dimensional array in java. instead of using arrayList. I am trying to rework on my code. I automate my app to select a game, but need to store in every row of data in string array....so i can loop through each row by row to check the game date, hometeam name and away team name whether matching or not ?

WebElement listView = AppSession.findElementByAccessibilityId("ListView1");
List<WebElement> row = listView.findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));
        ArrayList<ArrayList<String>> outerList = new ArrayList<ArrayList<String>>();
        for (int a = 0; a < row.size(); a++) {
            ArrayList<String> innerList = new ArrayList<String>();
            List<WebElement> column = row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));
            if(!column.isEmpty()){
                for (int j = 0; j < column.size(); j++)
                {
                    innerList.add(column.get(j).getAttribute("Name"));
                }
                outerList.add(innerList);
            }
        }
        System.out.println(outerList);



for (int i = 0; i < outerList.size(); i++) {
        if ((outerList.get(i).contains(game_date)) && (outerList.get(i).contains(home_team_name)) && (outerList.get(i).contains(away_team_name))) {
            WebElement line_up1 = AppSession.findElementByName(game_date);
            line_up1.click();
        }
    }

this is how my gui app looks like for game selection...

 Game date                   home team             away team
    06/29/2018 07:00:00 PM     Ball Hogs Ball Hogs    Tri-State Tri-State
    06/29/2018 08:00:00 PM     Power                  Ghost BALLERS
    06/29/2018 07:00:00 PM     Killer 3's             3's Company

outer list like this ::

[[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State],[06/29/2018 08:00:00 PM, Power, Ghost BALLERS],[06/29/2018 07:00:00 PM, Killer 3's, 3's Company]]

How to use multi dimensinal array instead of using array list in java

                WebElement listView = AppSession.findElementByAccessibilityId("ListView1");        
                List<WebElement> row =listView.findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));
                String[][] records = new String[row.size()][5];//11
                System.out.println(row.size());//11
                for(int a = 0; a < row.size(); a++)
                {
                    List<WebElement> column = row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));              
                    if(!column.isEmpty()){
                    for(int j = 0; j < 5; j++){
                        records[a][j] = column.get(j).getAttribute("Name");
                       System.out.println(records[a][j]);


                    }

                }
                }

System.out.println(records.length); //row count 10 System.out.println(records[0].length); //column count 5

    for(int lineNum=5;lineNum<records.length;lineNum++) {

        for(int colNum=0;colNum<5;colNum++)
        {

         if ((records[lineNum][colNum].contains(game_date)) && (records[lineNum][colNum].contains(home_team_name)) && (records[lineNum][colNum].contains(away_team_name))) 
         {

             WebElement line_up1 = AppSession.findElementByName(game_date);
             line_up1.click();
             AppSession.findElementByAccessibilityId("btnDelete").click();
             AppSession.findElementByName("Yes").click();
             WebDriverWait wait1 = new WebDriverWait(AppSession, 5);
             wait1.until(ExpectedConditions.visibilityOfElementLocated(By.name("OK")));
             AppSession.findElementByAccessibilityId("2").click();
             line_up1.click();
             AppSession.findElementByAccessibilityId("btnOk").click(); 


         }
     }  
    }

output

06/22/2018 09:00:00 PM
Indiana Fever
Seattle Storm
WNBA
5
07/22/2018 02:00:00 PM
Seattle Storm
Atlanta Dream
WNBA
5
08/01/2018 09:00:00 PM
Phoenix Mercury
Las Vegas Aces
WNBA
5

it is click on eclipse top most left restore view.. it is not click on the respective game..why ?

this is the data i am getting,I have data from backend. like gamedate, hometeamname and awayteam name.. i am stored in 3 variables. but how to do check with gamedate, hometeam, awayteam. whether if it matches then i need to select ?

  • What is the problem you are trying to solve with a multi-dimensional array? – V.A. Sep 9 at 12:55
up vote 1 down vote accepted

First of all you should clearly understand why you need multidimensional array here. There is a number of aspects that makes using arrays conceptually different from using lists.

Thus when you create an array you have to know which size that array would have. Unlike the lists which allow you to dynamically add new elements.

Multi-dimensional arrays are defined in the way:

String[][] records = new String[10][3];

which means you're creating 2-d array or in other word an array of arrays. In above example you're creating an array of 10 items each of which contains an array of 3 items.

Summary:

  1. You should evaluate how many entries are there on your UI (numberOfRows)
  2. You cannot have variably number of columns so that you have to define the number of columns each line will contain (numberOfCols)
  3. You then populate the array using the loop like described below:

-

String[][] records = new String[numberOfRows][numberOfCols];

for(int i = 0; i < numberOfRows; i++){
    List<WebElement> column = row.get(i).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));
    for(int j = 0; j < numberOfCols; j++){
        records[i][j] = column.get(j).getAttribute("Name");
    }
}

-

  1. Then you just check your conditions using the access to the array elements like records[lineNum][colNum]
  • How do you think what could be the reason? – Alexey R. Sep 12 at 14:21
  • please let me know whether i correct for comparison? or any other approach like compare 2d array with single array....single array (db data) – Anthony Sep 14 at 2:27
  • I believe this is not connected with arrays approach. looks like it's something with how you locate elements. – Alexey R. Sep 14 at 7:10
  • @ Alexey R. selecting the respective match is taking more time.. any other way like 2d array with 1d array comparison – Anthony Sep 14 at 7:16
  • 2
    records[lineNum][rowNum]? I guess, you meant records[lineNum][columnNum]. – dzieciou Sep 14 at 8:43

As mostly number of columns will be constant, so why not use List of the String array.

Simple Usage Example:

 import java.util.*;
    //...

    List<String[]> rowList = new ArrayList<String[]>();

    rowList.add(new String[] { "a", "b", "c" });
    rowList.add(new String[] { "d", "e", "f" });
    rowList.add(new String[] { "g", "h" });

    for (String[] row : rowList) {
        System.out.println("Row = " + Arrays.toString(row));
    } // prints:
      // Row = [a, b, c]
      // Row = [d, e, f]
      // Row = [g ,h]

    System.out.println(rowList.get(1)[1]); // prints "e"

By using List, the number of rows can grow and shrink dynamically also.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.