Java for循环解释
我希望能在程序中找到一些关于这段代码的帮助,但我不能理解它,我已经通过注释猜到了它的作用,但是如果我错了,请纠正我Java for循环解释,java,for-loop,Java,For Loop,我希望能在程序中找到一些关于这段代码的帮助,但我不能理解它,我已经通过注释猜到了它的作用,但是如果我错了,请纠正我 for(String[] movieArray:movie) { for(String data:movieArray) { if(data!=null){ //If data is not empty then it writes... jTextAr
for(String[] movieArray:movie)
{
for(String data:movieArray)
{
if(data!=null){ //If data is not empty then it writes...
jTextArea1.append(data+", "); //...this to the textarea.
}
else{ //If data is empty, then it will stop.
empty=true;
break;
}
}
if(empty==false){ //??
jTextArea1.append("\n");
}
}
}
在数组
movieArray
中的所有元素都不是null
之后,它们将被附加到jTextArea1
,并且空的将保持false
(前提是它最初是false
)
在
的内部结束后,如果empty
为false
(如果满足第一条语句中的条件,就会发生这种情况),如果empty
设置为true
(数组中有null
元素),则会追加一个新行字符(\n
),然后它将不会打印新行字符
下面是如何通过一个例子更好地理解它
movie = {{"1", "2", "3"}, {"4", "5", "6"}}; // Example 1
jTextArea1
将
1, 2, 3,
4, 5, 6,
1, 4, 5, 6,
如果
movie = {{"1", null, "3"}, {"4", "5", "6"}}; // Example 2
jTextArea1
将
1, 2, 3,
4, 5, 6,
1, 4, 5, 6,
这是因为在第二种情况下,数组中的一个元素是null
,因此在将设置为空
为真
后,它脱离了for
。由于空是真的
,因此它没有打印新行字符。您的评论是正确的
/**if no one of the data objects is empty, the boolean `empty` is
*still false and then a \n is added to the textarea.
*/
if(empty==false){
jTextArea1.append("\n");
}
empty==false
与相同!空的
在我看来像坏了一样
if(empty==false){ //??
jTextArea1.append("\n");
}
应该在for循环中
for(String[] movieArray:movie)
这意味着-循环将继续,直到电影数组中存在一个值。
相当于
for(int i =0; i<movie.length();i++)
String [] movieArray = movie[i];
什么类型的对象是movie
?顺便说一句,如果(!empty)
@AJ,最好将其更改为if(!empty)
@AJ。看起来像字符串[][],我认为user3042022与增强的for混淆了loop@AJOP的问题并不是说下面的答案是什么。