for循环与foreach循环遍历数组的区别

在遍历数组的时候,for循环能够在遍历过程中修改数组中元素,而foreach不行

例如(在main方法中):int[] array=new int[3];

                                        for(int i=0;i<3;i++){

                                       array[i]=i;}

                                       for(int i=0;i<3;i++){

                                       array[i]=i+1;

                                       System.out.println(array[i]+" ");//结果为1  2  3

                                       for(int i:aray){

                                       System.out.println(i+" ");//结果为0  1  2

猜你喜欢

转载自blog.csdn.net/weixin_42565135/article/details/82929161