请向我解释这个for循环[关闭]

问题描述 投票:0回答:3

您能解释一下这个for-loop吗?

beanNote bnote = new beanNote();
String somme=0;
for (Note note : bnote.getNotes()) {
     somme = somme + note.getNoteMat();
}

我只知道经典的for-loop例如:

for(int i=0; i<1000; i++){
   // do job .....
}
java java-ee
3个回答
4
投票

它等效于:

for(Note note = bnote.getFirstNote(); bnote.stillNotes(); note = bnote.getTheFolowingNote()){

 somme = somme + note.getNoteMat(); 

}


3
投票
for (Note note : bnote.getNotes()) {
     somme = somme + note.getNoteMat();
}

它的nhanced for-loop有时甚至被称为Java版本5中引入的for-each loop ,这使得遍历集合和数组变得灵活。

每次循环


2
投票

这是一个for-each构造:bnote.getNotes()返回一个数组或Iterable对象,并且对该集合中的每个对象执行循环的主体。

© www.soinside.com 2019 - 2024. All rights reserved.