0

我正在运行我的代码,在它说出第一个打印语句之后它会暂停。它在调用函数“插入”的地方暂停,并且根本不响应任何内容。它打印“添加狗、猫和马”,但随后停止,之后不做任何事情。

主功能

package assignment2;
public class Main {
    public static void main(String[] args) {
        OrderedStringList myList = new OrderedStringList(5);

        System.out.println("adding dog, cat, & horse");
        myList.Insert("dog");
        myList.Insert("cat");
        myList.Insert("horse");

        myList.Display();

        System.out.println("Value pig find = "+ myList.Find("pig"));
        System.out.println("Value horse find = "+ myList.Find("horse"));

        System.out.println("Adding mouse & rat");
        myList.Insert("mouse");
        myList.Insert("rat");

        myList.Display();

        System.out.println("myList size: "+ myList.Size());

        if (!myList.Insert("chinchilla"))
            System.out.println("Could not add chinchilla, full");

        System.out.println("Removing dog, adding chinchilla.");
        myList.Delete("dog");
        myList.Insert("chinchilla");

        myList.Display();
    }
}

这是我的函数代码

package assignment2;
public class OrderedStringList {
    int length;
    int numUsed;
    String[] storage;
    boolean ordered;

    public OrderedStringList(int size){
        length = size;
        storage = new String[length];
        numUsed = 0;
    }


    public boolean Insert(String value){
        boolean result = false;
                int index = 0;
                if (numUsed < length) {
                    while (index < numUsed) {
                        int compare = storage[index].compareTo(value);
                        if (compare < 0)
                            index++;
                    }
                    moveItemsDown(index);
                    storage[index] = value;
                    numUsed++;
                    result = true;
                }
                return result;
    }
    private void moveItemsDown(int start){
        int index;
        for (index = numUsed-1; index >=start; index--){
            storage[index+1] = storage[index];
        }
    }

    private void moveItemsUp(int start){
        int index;
        for (index = start; index < numUsed-1; index++){
            storage[index] = storage[index+1];
        }
    }

    public boolean Find(String value){
        return (FindIndex(value) >= 0);
    }

    private int FindIndex(String value) {
        int result = -1;
        int index = 0;
        boolean found = false;
        while ((index < numUsed) && (!found)) {
            found = (value.equals(storage[index]));
            if (!found)
                index++;
        }
        if (found)
            result = index;
        return result;
    }

    public boolean Delete(String value){
        boolean result = false;
        int location;
        location = FindIndex(value);
        if (location >= 0) {
            moveItemsUp(location);
            numUsed--;
            result = true;
        }
        return result;
    }

    public void Display() {
        int index;
        System.out.println("list Contents: ");
        for (index = 0; index < numUsed; index++) {
            System.out.println(index+" "+storage[index]);
        }
        System.out.println("-------------");
        System.out.println();
    }

    public void DisplayNoLF() {
        int index;
        System.out.println("list Contents: ");
        for (index = 0; index < numUsed; index++) {
            System.out.print(storage[index]+" ");
        }
        System.out.println("-------------");
        System.out.println();
    }

    public int Size(){
        return numUsed;
    }
}
4

2 回答 2

7

在 Insert 函数的 while 语句中,您陷入了无限循环。考虑这段代码:

while (index < numUsed) {
    int compare = storage[index].compareTo(value);
    if (compare < 0)
        index++;
}

如果compare >= 0为会发生什么index = 0?索引不会向上增加,然后在无限期再次调用 while 循环index = 0。您需要在 if 语句之外增加 index 并在 if 语句中放置不同的条件。

于 2013-02-18T22:16:12.307 回答
0
            while (index < numUsed && storage[index].compareTo(value) < 0) {
                    index++;
            }

通过这样做解决了我的问题。我只是删除了 for 循环并在 while 循环上添加了一个额外的要求。

于 2013-02-18T22:58:00.570 回答