gpt4 book ai didi

java - 为什么我的 Java 代码会导致 netbeans 卡住

转载 作者:行者123 更新时间:2023-12-01 06:45:51 25 4
gpt4 key购买 nike

我一直在做一项工作,当我尝试运行它时,我的 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() 将纠正该问题。

关于java - 为什么我的 Java 代码会导致 netbeans 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14342660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com