-2

I don't know why this piece of code is not passing.

Return true if each sub-list is individually in ascending order, false otherwise.

public static boolean allSubListsAscending(ArrayList<ArrayList<Integer>> list) {

        for (ArrayList<Integer> intList : list) {
            for (int j = 0; j < intList.size() - 1; j++) {
                if (intList.get(j) > intList.get(j + 1))
                    return false;
            }

        }
        return true;
}

@Test @Graded(description="AllSubListsAscendingComprehensive")
    public void testAllSubListsAscendingComprehensive() {
        testAllSubListsAscendingBasic();
        assertFalse(ListOfListService.allSubListsAscending(null));
        assertFalse(ListOfListService.allSubListsAscending(list4_nullItems));

        ArrayList<ArrayList<Integer>> ascending = new ArrayList<ArrayList<Integer>>();
        ascending.add(new ArrayList<Integer>(Arrays.asList(10, 20, 70, 90)));
        ascending.add(new ArrayList<Integer>(Arrays.asList(100, 110, 120, 130)));
        ascending.add(null);
        ascending.add(new ArrayList<Integer>(Arrays.asList(10, 20, 70, 90)));
        assertFalse(ListOfListService.allSubListsAscending(ascending));

        currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
    }
  • Please post an SSCCE. You can read what it is from here: http://sscce.org/ – Doga Oruc May 08 '21 at 02:46
  • add if(list ==null) return false; in the beginning of your method. and if(intList ==null) return false; inyour first for loop – Angel Koh May 08 '21 at 02:54
  • +1 for sscce. However the very first test case will cause your method to throw a null pointer exception, not return false. – Gene May 08 '21 at 02:54

1 Answers1

0

you have null value in your sublist, but you didn't process null case in allSubListsAscending.

the allSubListsAscending method should be like this:

    public static boolean allSubListsAscending(ArrayList<ArrayList<Integer>> list) {

    if(null == list){
        return false;
    }
    for (ArrayList<Integer> intList : list) {
        if(null==intList){
            return false;
        }
        for (int j = 0; j < intList.size() - 1; j++) {
            if (intList.get(j) > intList.get(j + 1))
                return false;
        }

    }
    return true;
}
Dong Hang
  • 9
  • 5