为什么我的Java代码导致netbeans冻结?



我一直在做一件工作,当我试图运行它时,我的IDE冻结了,我不得不使用任务管理器关闭它。

知道为什么吗?我知道它与迭代器和while循环有关。

有人能发现吗?下面部分servlet代码:

try {
        List<FileItem> fields = upload.parseRequest(request);
        Iterator<FileItem> it = fields.iterator();


        //starting conversion from file to sample

        //creating a container to hold the samples
        ArrayList<sample> samplelist = new ArrayList();
        while (it.hasNext()) {
            sample c = new sample();
            FileItem fileItem = it.next();
            c.setFileName(fileItem.getName());
            c.setPayload(fileItem.getString());
            samplelist.add(c);
        }
        //looping through uploaded samples to split code into lines
        for (int i = 0; i < samplelist.size(); i++) {
            sample current = new sample();
            current = samplelist.get(i);
            //splitting whole code block into lines based on ;
            String[] temp;
            temp = current.getPayload().split(";");
            //going through split lines and adding them to the sample's rawcode
            //arraylist after checking for for loops
            for (int j = 0; j < temp.length; j++) {
                // current.Raw_Code.add(temp[j]);
                String current_line = temp[j];


                current.Raw_Code.add(current_line);
            }


        }
//testing
        System.out.print("starting testing");
        for (int i = 0; i < samplelist.size(); i++) {
            sample current = new sample();
            current = samplelist.get(i);
            System.out.print("File name <br/>");
            current.getFileName();
            System.out.print("lines detected <br/>");
            Iterator itr = current.Raw_Code.iterator();
           //this while loop started to cause the problem
             while(itr.hasNext()){
             System.out.print("x");   
             }
        }


    } catch (FileUploadException e) {
        e.printStackTrace();
    }

    out.close();
    return;

右边:

while(itr.hasNext()){
    System.out.print("x");   
}

你没有调用itr.next(),所以迭代器没有迭代。:)在你的while循环中添加itr.next()将纠正这个问题。

你的while是一个无限循环-迭代器总是有next,因为它从不迭代…所以它一直在继续。

另一个注意事项是,每当IDE崩溃时,您应该考虑"无限循环",因为它是该编程问题的常见副作用。

在该位置缺少itr.next()

相关内容

  • 没有找到相关文章

最新更新