我的方法检查字符串数组是否排序有什么问题

时间:2016-08-31 12:36:51

标签: java arrays sorting arraylist

我正在做一个Java类,我无法弄清楚我错在哪里。

我有一个名为ArrayMethods的类和一个必须用来查看我的数组是否已排序的方法。这是我的代码:

public class ArrayMethods
{
    String[] list; //instance variable
    /**
     * Constructor for objects of class ArrayMethods
     */
    public ArrayMethods(String[] list)
    {
        // initialise instance variables
        this.list = list;
    }

    /**
     * Determines if the array is sorted (do not sort)
     * When Strings are sorted, they are in alphabetical order
     * Use the compareTo method to determine which string comes first
     * You can look at the String compareTo method in the Java API
     * @return true if the array  is sorted else false.
     */
    public boolean isSorted()
    {
        boolean sorted = true;

        // TODO: Write the code to loop through the array and determine that each
        // successive element is larger than the one before it
        for (int i = 0; i < list.length - 1; i++){
            if (list[i].compareTo(list[i + 1]) < 0){
                sorted = true;
            }
        }
        return sorted;
    }
}

然后我有一个这样的数组测试器:

public class ArrayMethodsTester {
    public static void main(String[] args) {
        //set up
        String[] animals = {"ape", "dog", "zebra"};
        ArrayMethods zoo = new ArrayMethods(animals); 

        //test isSorted
        System.out.println(zoo.isSorted());
        System.out.println("Expected: true");

        String[] animals2 = {"ape", "dog", "zebra", "cat"};
        zoo = new ArrayMethods(animals2);         
        System.out.println(zoo.isSorted());
        System.out.println("Expected: false");

        String[] animals3 = {"cat", "ape", "dog", "zebra"};
        zoo = new ArrayMethods(animals3); ;
        System.out.println(zoo.isSorted());
        System.out.println("Expected: false");
    }
}

对于第一个数组我确实是正常的,因为它是正常的,问题是我对其他2得到了真的,显然这是错误的。什么是我没有得到的?

3 个答案:

答案 0 :(得分:2)

可以通过直接在循环内部返回Flyout来使其更简单一些

<Controls:Flyout x:Name="Flyout" Header="Flyout" Grid.Row="1">

</Controls:Flyout>

答案 1 :(得分:1)

public class ArrayMethods
{
    String[] list; //instance variable
    /**
     * Constructor for objects of class ArrayMethods
     */
    public ArrayMethods(String[] list)
    {
        // initialise instance variables
        this.list = list;
    }

    /**
     * Determines if the array is sorted (do not sort)
     * When Strings are sorted, they are in alphabetical order
     * Use the compareTo method to determine which string comes first
     * You can look at the String compareTo method in the Java API
     * @return true if the array  is sorted else false.
     */
    public boolean isSorted()
    {
        boolean sorted = true;

        // TODO: Write the code to loop through the array and determine that each
        // successive element is larger than the one before it
       for (int i = 0; i < list.length - 1; i++){
            if (list[i].compareTo(list[i + 1]) > 0){
                sorted = false;
                break;

            }
        }
        return sorted;
    }
}`

答案 2 :(得分:0)

如果您喜欢它们的语法,您也可以使用Java 8流(虽然它不是一个完美的用例,因为您需要流的两个元素用于您的操作):

public static boolean isSorted(final String[] array) {
    return !IntStream.range(1, array.length)
        .mapToObj(i -> new Pair<String>(array[i - 1], array[i])).parallel()
            .anyMatch(t -> t.first.compareTo(t.second) > 0);
}

代码使用小帮助程序类Pair

public static final class Pair<T> {
    final T first;
    final T second;

    private Pair(final T first, final T second) {
        this.first = first;
        this.second = second;
    }
}

此解决方案也可以并行运行,这样可以在大型阵列上运行时更快。

Collect successive pairs from a stream致使用流来访问一对元素