sorting an Array List, Precondition solution

542 views Asked by At

I have a small project which allows me to create an Array List (storing integer values called numbers). there is one method which takes all elements within the Array and organizes it according to the int value. e.g [3, 2, 9, 7] would be [2, 3, 7, 9] after running numbers.sort(). My Task is to write a Precondition for the method sort(). My thinking was that you'd want to add a Precondition to sort() that only accepts an Array List that has 2 or more elements because running the sort function with an empty or single element Array List is pretty pointless. I already have conditions which state the method can't accept null Array Lists or null elements within it. would adding the above mentioned Precondition (2 or more elements required) be the right call ?

Method:

@Requires("numbers.size() > 1") // The Precondition)
@Ensures("isOrdered(numbers)") // ignore this)
public void sort() {
    Collections.sort(numbers);
}
0

There are 0 answers