码迷,mamicode.com
首页 > 编程语言 > 详细

关于Java for循环的注意点

时间:2014-09-28 10:39:31      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   for   sp   div   c   

 1 import java.util.ArrayList;
 2 
 3 public class Main {
 4     
 5     public static void main(String[] args) {
 6         ArrayList<Integer> list = new ArrayList<>();
 7         list.add(1);
 8         list.add(2);
 9         list.add(3);
10         list.add(4);
11         
12         for(int n:list){
13             n = 4;//此种做法不会改变list的元素
14             //System.out.println(n);
15         }
16         /* 增强型for循环*/
17         for (int m:list) {
18             System.out.print(m + " ");
19         }
20         System.out.println();
21         
22         for(int i = 0; i < list.size();i++){
23             //list.add(i);
24             list.remove(i); //在for循环中,list的大小是不好的做法。
25         }
26         
27         for (int m:list) {
28             System.out.print(m+" ");
29         }
30     }
31 }

 

关于Java for循环的注意点

标签:style   blog   color   ar   java   for   sp   div   c   

原文地址:http://www.cnblogs.com/wanghui390/p/3997694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!