这个数组的代码出了什么问题?

时间:2016-06-03 10:29:50

标签: java arrays compare

我正在进行在线课程,但我遇到了问题。我不明白我的代码有什么问题。你能看看并给我一个暗示吗?我不明白为什么最后一次测试不起作用。当他们将pos 0中的cat添加到列表中时,列表应该变成[cat,ape,dog,zebra],不是吗?

    import java.util.ArrayList;

public class ArrayListMethods
{
    ArrayList<String> list; //instance variable
    /**
     * Constructor for objects of class ArrayListMethods
     */
    public ArrayListMethods(ArrayList<String> arrayList)
    {
        // initialise instance variables
        list = arrayList;
    }

    /**
     * Determines if the array list 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 list is sorted else false.
     */
    public boolean isSorted()
    {
        boolean sorted = true;

        // TODO: Determine if the array is sorted.
        for (int i = 0; i < list.size() - 1; i++){
            if (list.get(i).compareTo(list.get(i + 1)) < 0){
                sorted = true;

            }
            else {
                sorted = false;
            }
        }

        return sorted;
    }
}

用于测试代码的测试器类具有:

    import java.util.ArrayList;
public class ArrayListMethodsTester
{
    public static void main(String[] args)
    {
        //set up
        ArrayList<String> animals = new ArrayList<String>();
        ArrayListMethods zoo = new ArrayListMethods(animals); 
        zoo.list.add("ape");
        zoo.list.add("dog");
        soo.list.add("zebra");

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

        zoo.list.add("cat");
        System.out.println(zoo.isSorted());
        System.out.println("Expected: false");

        zoo.list.remove("cat");
        zoo.list.add(0,"cat");
        System.out.println(zoo.isSorted());
        System.out.println("Expected: false");
}
}

2 个答案:

答案 0 :(得分:2)

如果最后两个元素未排序,则l6.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { l6MouseClicked(e); } private void l6MouseClicked(MouseEvent e) { try { // TODO add your handling code here: Process p = Runtime.getRuntime().exec("cmd /c start shell:startmenufolder"); } catch (IOException ex) { Logger.getLogger(Dsk1.class.getName()).log(Level.SEVERE, null, ex); } } }); 方法仅返回false。将isSorted变量设置为false后,您应添加break

sorted

或更简单:

public boolean isSorted() {
    boolean sorted = true;

    // TODO: Determine if the array is sorted.
    for (int i = 0; i < list.size() - 1; i++){
        if (list.get(i).compareTo(list.get(i + 1)) < 0){
            sorted = true;

        }
        else {
            sorted = false;
            break; // Add break here
        }
    }

    return sorted;
}

答案 1 :(得分:1)

问题出在你的isSorted()方法中,你在第一次迭代中将结果设置为false(因为cat没有排序)。但是在第二次迭代中,它将结果设置为true,因为&#34; ape&#34;与&#34; dog&#34;

进行排序

解决方案是在错误成立后完成该过程。

所以改变这个:

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

为此:

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

它可以正常工作,并且还可以提高性能,因为不需要检查整个阵列。如果前几个元素未排序,则表示数组未排序