How to use multi dimensional array in java instead of using array list?



  • 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 ?



  • 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]

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2