5

I need to estimate if the array list is sorted (don't sort).

When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first

And return true if the array list is sorted, else 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;
}

Easy 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 easy test shows only 1/3 coverage.

How to solve this issue.

4

6 回答 6

9

您的方法中有一个小错误。应该 :

public boolean isSorted()
{
    boolean sorted = true;        
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
    }

    return sorted;
}

>0而不是!=1,你不能确定它1被退回了..

于 2013-07-06T06:44:38.020 回答
3

改变条件:

if (list.get(i - 1).compareTo(list.get(i)) >0)

您应该检查>0而不是!=-1.

浏览compareTo()的文档

如果参数字符串等于该字符串,则值为 0;如果此字符串按字典顺序小于字符串参数,则值小于 0;如果此字符串按字典顺序大于字符串参数,则值大于 0

于 2013-07-06T06:47:23.723 回答
1

尝试这个

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sort {
public static void main(String []args) {
    List<String> l1=new ArrayList<String>();
    List<String> l2=new ArrayList<String>();
    l1.add("a");
    l1.add("b");
    l1.add("c");

    l2.add("b");
    l2.add("c");
    l2.add("a");

     if(isSorted(l1)){
         System.out.println("already sorted");
     }
    else{
         Collections.sort(l1);
     }
   }
public static boolean isSorted(List<String> list){
    String previous = "";
    for (String current: list) {
        if (current.compareTo(previous) < 0)
            return false;
        previous = current;
    }
    return true;
}
}
于 2013-07-06T06:51:44.227 回答
1

您可以编写一个实用方法,例如isSortedList(List list).

public static boolean isSortedList(List<? extends Comparable> list)
{
    if(list == null || list.isEmpty())
        return false;

    if(list.size() == 1)  
        return true;

    for(int i=1; i<list.size();i++)
    {
        if(list.get(i).compareTo(list.get(i-1)) < 0 )
            return false;
    }

    return true;    
}

作为实用方法,您可以在任何地方使用它。

于 2013-07-06T07:03:09.540 回答
0

您必须将 compareTo 表达式更改为任何正数,这表示前一个元素按字母顺序排列在当前元素之后,因此列表没有排序

    public boolean isSorted()
    {
        boolean sorted = true;        
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
        }

        return sorted;
    }
于 2013-07-06T06:50:34.413 回答
0

您需要检查未排序的情况。

这意味着,如果您假设升序排序,则未排序的情况将是在 index 处找到一个元素,该元素与 index wherei乱序。i-1element[i] < element[i-1]

于 2013-07-06T06:51:22.047 回答