Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java代码在执行中冻结_Java - Fatal编程技术网

Java代码在执行中冻结

Java代码在执行中冻结,java,Java,我正在运行我的代码,当它说出第一个print语句后,它会暂停。它会在调用函数“insert”时暂停,并且不会响应任何内容。它打印了“添加狗、猫和马”,但随后就停止了,之后什么也不做 主要功能 package assignment2; public class Main { public static void main(String[] args) { OrderedStringList myList = new OrderedStringList(5);

我正在运行我的代码,当它说出第一个print语句后,它会暂停。它会在调用函数“insert”时暂停,并且不会响应任何内容。它打印了“添加狗、猫和马”,但随后就停止了,之后什么也不做

主要功能

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;
    }
}
包分配2;
公共类OrderedStringList{
整数长度;
内特怒不可遏;
字符串[]存储;
布尔序;
public OrderedStringList(整数大小){
长度=尺寸;
存储=新字符串[长度];
numUsed=0;
}
公共布尔插入(字符串值){
布尔结果=假;
int指数=0;
如果(numUsed<长度){
而(索引=开始;index--){
存储[索引+1]=存储[索引];
}
}
私有void moveItemsUp(int start){
整数指数;
对于(索引=开始;索引=0);
}
私有int FindIndex(字符串值){
int结果=-1;
int指数=0;
布尔值=false;
而((索引=0){
moveItemsUp(位置);
傻傻的--;
结果=真;
}
返回结果;
}
公共空间显示(){
整数指数;
System.out.println(“列表内容:”);
对于(索引=0;索引
插入函数的while语句将陷入无限循环。考虑这段代码:

while (index < numUsed) {
    int compare = storage[index].compareTo(value);
    if (compare < 0)
        index++;
}
while(索引
如果
索引=0
比较>=0
会发生什么情况?索引不向上递增,然后在
Index=0
上无限再次调用while循环。您需要在if语句之外增加索引,并在if语句中添加不同的条件。

while(index            while (index < numUsed && storage[index].compareTo(value) < 0) {
                    index++;
            }
索引++; }
通过这样做解决了我的问题。我只是删除了for循环,并在while循环上添加了一个额外的要求。

使用调试器或在
Insert(…)
方法中放置一些println语句来查看发生了什么,特别是在while循环中。我认为这里的关键问题不是这个孤立问题的解决方案,而是您希望了解如何在将来调试这个和类似的问题。这一点的关键是知道程序运行时内部发生了什么。通常不赞成在此处发布作业供我们解决……注意:使用调试器很容易发现这一点,正如HoverCraftfullOfels answer ^^^中所建议的那样。下次遇到这种问题时,请尝试一下。我没有尝试过,但如果我看到代码的话。我认为它不会进入while循环。如果我错了,请纠正我!!索引和numUsed都是0。它是如何进入while的?当它插入dog时,它不会进入while循环。但当他把猫放进去的时候,它就进去了,再也出不来了@卤素得到了正确的点,但是在第二次调用insert方法时+小伙子,你把事情解决了!如果我的回答对你有帮助,请接受我的回答,因为下一个可怜的灵魂也会遇到类似的问题。