2016-08-31 20 views
0

我正在做一個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"); 
    } 
} 

對於第一陣列我得到真正的,因爲它是正常的,問題是,我對其他兩個人都是正確的,顯然這是錯誤的。我沒有得到什麼?

+4

'sorted'始終是'真',因爲你永遠不指定'FALSE'它。 – talex

+0

您是否嘗試過[調試您的方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? –

回答

2

可以使由循環內直接返回false它稍微簡單

for (int i = 0; i < list.length - 1; i++) { 
     if (list[i].compareTo(list[i + 1]) > 0) { 
      return false; 
     } 
    } 
    return true; 
+0

謝謝,這工作就像一個魅力! –

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; 
    } 
}` 
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訪問元素的配對流

+0

我很確定它可以處理重複的條目並通過多次測試進行驗證。你可以舉一個例子來說明它不起作用的情況嗎? –

+0

啊,我明白了,你在這裏用否定來工作......錯過了。 – Tom

相關問題