`
azrael6619
  • 浏览: 574814 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java中的循环

阅读更多

1.while循环:

int i = 1;
int sum = 0;
while (i < =100) {
    sum = sum + i;
    i++;
}

 

 

2.do-while循环:

int i = 1;
int sum = 0;
do {
    sum = sum + i;
    i++;
} while (i <= 100);

 

 

3.for循环:

int i;
int sum = 0;
for (i=1;  i<=100;  i++) {
     sum = sum + i;
}

 

 

4.iterator循环:

public void testForLoop(PrintStream out) throws IOException {
  List list = getList();  // initialize this list elsewhere
  
  for (Iterator i = list.iterator(); i.hasNext(); ) {
    Object listElement = i.next();
    System.out.println(listElement.toString());
    
    // Do something else with this list element
  }
}

 

 

5.for/in循环:

public void testForInLoop(PrintStream out) throws IOException {
  List list = getList();  // initialize this list elsewhere
  
  for (Object listElement : list) {
    System.out.println(listElement.toString());
    
    // Do something else with this list element
  }
}

 

如果要输出map所有值,那么先要把map转化成Connection。

例如:

   import java.util.Collection;

 

   Collection col = map.values();

   //循环部分可以参考上面
   Iterator it = col.iterator();
   while (it.hasNext()) {
    System.out.println(it.next());
   }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics