How to compare 2d array with 1 d array?



  • I am selecting a respective date. I want to compare 2d array with 1 d array.. but i tried but its not getting for me..

     String[] array1= {"06/22/2018 09:00:00 PM","Indiana Fever","Seattle Storm"};
    
                            System.out.println(array1.length); //3
    
                            for (int i = 0; i < records.length; i++) {
    
                                if(java.util.Arrays.equals(array1, records[i])){
    
                                    WebElement line_up1 = AppSession.findElementByName(game_date);
                                     line_up1.click();
                                }
                            }
    

    in my records contains ::

    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
    07/20/2018 06:00:00 PM
    Tri-State Tri-State
    Ghost Ballers Ghost Ballers
    BIG3
    5
    

    records.length contains 4 and inside 5 column data, like records[0] - 5



  • When you compare arrays which have different length the comparison will return false. In your case you have array1->lenght 3 and the specific row in lets say records[0]-> lengh 5.
    If you remove element "WNBA" and "5" from your records array the comparison is seamless:

    //      String[][] records = new String[2][3];
    //      records[0][0] = "06/22/2018 09:00:00 PM";
    //      records[0][1] = "Indiana Fever";
    //      records[0][2] = "Seattle Storm";
    //      records[1][0] = "06/22/2018 09:00:00 PM";
    //      records[1][1] = "Indiana Fever";
    //      records[1][2] = "Atlanta Dream";
            String[][] records = {{"06/22/2018 09:00:00 PM", "Indiana Fever","Seattle Storm"},
                    {"06/22/2018 09:00:00 PM", "Indiana Fever","Atlanta Dream"}};
            String[] array1 = { "06/22/2018 09:00:00 PM", "Indiana Fever", "Seattle Storm" };
    
            System.out.println(array1.length); // 3
    
            System.out.println(records.length); // 2
    
            for (int i = 0; i < records.length; i++) {
    
                    System.out.println(java.util.Arrays.equals(array1, records[i]));
            }
    

    But if you want to keep your records array intact with 5 element and still want to compare with 3 elements array you can do the following:

    String[][] records = { { "06/22/2018 09:00:00 PM", "Indiana Fever", "Seattle Storm", "WNBA", "5" },
            { "06/22/2018 09:00:00 PM", "Indiana Fever", "Atlanta Dream", "WNBA", "5" } };
    String[] array1 = { "06/22/2018 09:00:00 PM", "Indiana Fever", "Seattle Storm" };
    
    System.out.println(array1.length); // 3
    
    System.out.println(records.length); // 2
    
    for (int i = 0; i < records.length; i++) {
        for (int j = 0; j < array1.length; j++) {
            if (!array1[j].equals(records[i][j]) {
                System.out.println("not equal");
                break;
            }
            if ((j == array1.length - 1) && (array1[j].equals(records[i][j]))) {
                System.out.println("equal");
            }
    
        }
    }
    

Log in to reply
 

Suggested Topics

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