我一直致力于一项工作,当我尝试运行它时,我的ide冻结,我必须使用任务管理器关闭它。
任何想法为什么? 我知道它与迭代器和我正在使用的循环
有关任何人都可以发现它吗? 下面是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;
答案 0 :(得分:10)
就在这里:
while(itr.hasNext()){
System.out.print("x");
}
您没有调用itr.next()
,因此迭代器不会迭代。 :)在itr.next()
循环内添加while
将解决问题。
答案 1 :(得分:1)
你的while是一个无限循环 - 迭代器总是hasNext因为它永远不会迭代......所以它一直在继续。
另一方面,每当IDE崩溃时,您应该认为“无限循环,因为它是该编程问题的常见副作用。”
答案 2 :(得分:-1)
在那个地方缺少itr.next()