我的代码对这个数组有什么问题?

时间:2023-01-04 22:50:18

I am doing a online course and I have a problem. i do not understand what is wrong with my code. Can you please take a look and give me a hint? I don't get why the last test is not working. When they add the cat in pos 0 to the list, the list should become [cat, ape, dog, zebra], no?

我正在做一个在线课程,我有一个问题。我不明白我的代码有什么问题。你能看看并给我一个提示吗?我不明白为什么最后一次测试不起作用。当他们将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;
    }
}

The tester class used to test the code has this:

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

    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 个解决方案

#1


1  

The problem is inside your isSorted() method, you are setting the result as false in the first iteration (because cat is not sorted). But in the second iteration it sets the result as true because "ape" is sorted compared with "dog"

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

The solution is to finish the process once a false is founded.

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

So change this:

所以改变这个:

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 this:

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

And it will work fine and it will also improve the performance, since there is no need to check the whole array. If the first couple of elements are not sorted, then the array is not sorted

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

#2


2  

You isSorted method only returns false if the last two elements are not sorted. You should add a break after setting the sorted variable to false:

如果最后两个元素未排序,则isSorted方法仅返回false。您应该在将sorted变量设置为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;
            break; // Add break here
        }
    }

    return sorted;
}

Or more simply:

或者更简单:

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

    return true;
}

#1


1  

The problem is inside your isSorted() method, you are setting the result as false in the first iteration (because cat is not sorted). But in the second iteration it sets the result as true because "ape" is sorted compared with "dog"

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

The solution is to finish the process once a false is founded.

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

So change this:

所以改变这个:

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 this:

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

And it will work fine and it will also improve the performance, since there is no need to check the whole array. If the first couple of elements are not sorted, then the array is not sorted

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

#2


2  

You isSorted method only returns false if the last two elements are not sorted. You should add a break after setting the sorted variable to false:

如果最后两个元素未排序,则isSorted方法仅返回false。您应该在将sorted变量设置为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;
            break; // Add break here
        }
    }

    return sorted;
}

Or more simply:

或者更简单:

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

    return true;
}