陷入 while 循环
我有一个像这样的简单代码:
try {
int i = 0;
while (i < size) {
System.out.println("i is" + i);
if (someCondition) {
System.out.println("do sth");
someCondition = true;
i++;
System.out.println("i is" + i);
} else {
System.out.println("doAnotherThing");
someCondition = true;
i++;
System.out.println("i is" + i);
}
}
} catch(Exception){
}
该代码片段的输出是:
i is 0
do sth
i is 1
i is 0
doAnotherThing
i is 1
它应该增加 I,而不是中断 while 循环,但事实并非如此。对于这个问题你有什么看法吗?我已经研究这个问题 5 个小时了,也许我遗漏了一些东西。如果你能帮助我,我会很高兴。
提前致谢。
编辑:我想简化事情,但看起来它不起作用:)好的这是真正的代码:
public void (Analyzer analyzer){
try {
int i=0;
while (i < analyzer.size()) {
System.out.println("i is" + i);
Object anInstance = analyzer.getObject().get(i);
if (anInstance.getDatabaseCreated()) { //this comes from another class and is //false in the first place
dropObject(analyzer.getId(),i); // removes object
createAnInstance(analyzer.getId(), i, anInstance.getTypes()); //creates another instance
anInstance.markCreated();
Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
for (int j = 0; j < anInstance.rowCount(); j++) {
insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
}
i++;
System.out.println("i is" + i);
} else {
createAnInstance(analyzer.getId(), i, anInstance.getTypes());
anInstance.markCreated();
Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
for (int j = 0; j < anInstance.rowCount(); j++) {
insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
}
i++;
System.out.println("i is" + i);
}
}
} catch (Exception ex) {
Logger.getLogger(AnalyzerService.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}
I have a simple code like this:
try {
int i = 0;
while (i < size) {
System.out.println("i is" + i);
if (someCondition) {
System.out.println("do sth");
someCondition = true;
i++;
System.out.println("i is" + i);
} else {
System.out.println("doAnotherThing");
someCondition = true;
i++;
System.out.println("i is" + i);
}
}
} catch(Exception){
}
This code snippet's output is:
i is 0
do sth
i is 1
i is 0
doAnotherThing
i is 1
It should have increase I, than break while loop but it doesn't. Do you have any opinion about this issue? I am working on this issue since 5 hours, maybe I am missing something. I will be glad if you can help me.
Thanks in advance.
EDIT: I wanted to simplify things, but appearantly it didn't work :) OK Here is the real code:
public void (Analyzer analyzer){
try {
int i=0;
while (i < analyzer.size()) {
System.out.println("i is" + i);
Object anInstance = analyzer.getObject().get(i);
if (anInstance.getDatabaseCreated()) { //this comes from another class and is //false in the first place
dropObject(analyzer.getId(),i); // removes object
createAnInstance(analyzer.getId(), i, anInstance.getTypes()); //creates another instance
anInstance.markCreated();
Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
for (int j = 0; j < anInstance.rowCount(); j++) {
insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
}
i++;
System.out.println("i is" + i);
} else {
createAnInstance(analyzer.getId(), i, anInstance.getTypes());
anInstance.markCreated();
Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
for (int j = 0; j < anInstance.rowCount(); j++) {
insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
}
i++;
System.out.println("i is" + i);
}
}
} catch (Exception ex) {
Logger.getLogger(AnalyzerService.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为为了获得您给出的输出,必须对该方法进行两次单独的调用(在上面的代码片段中没有名称)。看起来该方法被调用,i被设置为0,循环开始并且第一个条件为真。然后循环结束(因为analyzer.size()是1,或者可能是一个异常???)然后在代码的其他地方再次调用这个方法。 i 再次设置为 0,但这次第一个条件为 false(第一次调用该方法的结果???),因此您改为执行 else。同样,循环在 1 次迭代后结束,要么因为analyzer.size() 为 1,要么抛出异常)。
I think that in order to get the output you have given there must be 2 separate calls being made to the method (which in your code snippet above has no name). It seems the method is called, i is set to 0, the loop starts and the first condition is true. The loop then ends (because analyzer.size() is 1, or maybe an exception???) then somewhere else in your code you are calling this method again. Again i is set to 0 but this time the first condition is false (a result of the first call to the method???) so you go through the else instead. Again the loop ends after 1 iteration, either because analyzer.size() is 1 or an exception is thrown).
为什么在
if
和else
中将someCondition
设置为 true ?另外,检查someCondition
是否将i
的值重置为0
或其他值。如果不知道someCondition
是什么,就很难猜测。但如果它是一个将i
的值作为参数的函数,我会检查它。Why are you setting the
someCondition
to true in bothif
andelse
? Also, check if thesomeCondition
is resetting the value ofi
to0
or something. Without knowing what thesomeCondition
is, it is difficult to guess. But if it is a function taking the value ofi
as a a parameter, I will check that.这段代码是假的——括号甚至没有对齐。正如所写,假设您初始化了 size 变量,这将起作用,否则 size 可能很大(随机整数,如 384923492348),并且会进行大量迭代。如果您想要更好的答案,您可以修复代码吗(只需复制粘贴?:))
This code's bogus - the bracketing doesn't line up even. As written this would work assuming you initialized the size variable, otherwise size might be huge (random integer number like 384923492348) and it would do alot of iterations. Can you fix the code up if you want a better answer (just copy paste? :) )
在我看来,
dropObject
正在改变analyzer.size()
,而后者的大小首先是1
。It seems to me that
dropObject
is alteringanalyzer.size()
and that the size of the latter was1
in the first place.很明显,
analyzer.size()
正在发生变化。由于我不知道其余的来源,因此我无法确定是什么原因造成的。 的列表:担心当您向查询添加行,您可以简单地提前缓存分析器大小,但您可能会遇到麻烦,因为看起来您正在删除一些值,同时添加其他值。不过,如果这并不重要,那就很简单:
另外,我注意到有很多复制和粘贴的代码。糟糕的交易。你的整个 while 循环可能是:
It's apparent that
analyzer.size()
is mutating. Since I don't know the rest of the source, I can't know for sure what's causing it. Here's a list of possible culprits:If you don't need to worry about what happens when you add lines to the query, you could simply cache analyzer size ahead of time, but you might run into trouble as it looks like you are removing some values while simultaneously adding others. If this doesn't matter, though, it is as simple as:
Also, I noticed that there was a lot of copied and pasted code. Bad deal. Your entire while loop could be: