I need to evaluate if the list of arrays is sorted (not sort).
When the rows are sorted, they are in alphabetical order. I am trying to use compareTo () to determine which row will be first.
And return true if the list of arrays is sorted, otherwise false.
Code:
public boolean isSorted() { boolean sorted = true; for (int i = 1; i < list.size(); i++) { if (list.get(i-1).compareTo(list.get(i)) != 1) sorted = false; } return sorted; }
A simple test:
ArrayList<String> animals = new ArrayList<String>(); ArrayListMethods zoo = new ArrayListMethods(animals); animals.add("ape"); animals.add("dog"); animals.add("zebra"); //test isSorted System.out.println(zoo.isSorted()); System.out.println("Expected: true"); animals.add("cat"); System.out.println(zoo.isSorted()); System.out.println("Expected: false"); animals.remove("cat"); animals.add(0,"cat"); System.out.println(zoo.isSorted()); System.out.println("Expected: false"); **Output:** false Expected: true false Expected: false false Expected: false
This simple test shows only 1/3 coverage.
How to solve this problem.
source share