3

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 ?

2 Answers2

0

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]
Alexey R.
  • 11,590
  • 5
  • 19
  • 39
  • How do you think what could be the reason? – Alexey R. Sep 12 '18 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) –  Sep 14 '18 at 02:27
  • I believe this is not connected with arrays approach. looks like it's something with how you locate elements. – Alexey R. Sep 14 '18 at 07:10
  • @ Alexey R. selecting the respective match is taking more time.. any other way like 2d array with 1d array comparison –  Sep 14 '18 at 07:16
  • What exactly does take slow? Performance of UI interaction does not depend on how you approach to store elements. – Alexey R. Sep 14 '18 at 08:18
  • 2
    records[lineNum][rowNum]? I guess, you meant records[lineNum][columnNum]. – dzieciou Sep 14 '18 at 08:43
  • @AlexeyR. I just check to print hello inside if condition.. it is not printing and click on eclipse top on area.. –  Sep 14 '18 at 09:46
  • @AlexeyR. it is click on top most left in the eclipse..why not selecting game ? –  Sep 14 '18 at 10:10
  • I cannot give you any advice because 1 - I do not have the app you test, 2 - I do not have your code, 3 - looks like this is completely different issue that was originally raised in the question. – Alexey R. Sep 14 '18 at 10:15
  • @AlexeyR. it is working..i 'm just increment the column as +1 –  Sep 14 '18 at 12:12
0

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.

Vishal Aggarwal
  • 5,572
  • 2
  • 19
  • 36