数组的增强for
@Test
public void test1(){
Integer[] integers = new Integer[5];
for(int i=0;i<integers.length;i++){
integers[i] = i;
}
for (Integer integer : integers) {
System.out.println(integer);
}
}
输出
0
1
2
3
4
对应代码的.class
@Test
public void test1() {
Integer[] integers = new Integer[5];
for(int i = 0; i < integers.length; ++i) {
integers[i] = i;
}
Integer[] var6 = integers;
int var3 = integers.length;
for(int var4 = 0; var4 < var3; ++var4) {
Integer integer = var6[var4];
System.out.println(integer);
}
}
集合的增强for
@Test
public void test1(){
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(11);
list.add(1111);
list.add(11111);
for (Integer integer : list) {
System.out.println(integer);
}
}
集合增强for的.class
@Test
public void test1() {
ArrayList<Integer> list = new ArrayList();
list.add(1);
list.add(11);
list.add(1111);
list.add(11111);
Iterator var2 = list.iterator();
while(var2.hasNext()) {
Integer integer = (Integer)var2.next();
System.out.println(integer);
}
}
结论:
集合的增强for底层使用迭代器
数组增强for的时候改变
@Test
public void test2(){
Integer[] integers = new Integer[5];
for(int i=0;i<integers.length;i++){
integers[i] = i;
}
for (Integer integer : integers) {
if(integer==2){
integer=2222222;
}
}
for (Integer integer : integers) {
System.out.println(integer);
}
}
数组增强for改变时候的class
@Test
public void test2() {
Integer[] integers = new Integer[5];
for(int i = 0; i < integers.length; ++i) {
integers[i] = i;
}
Integer[] var6 = integers;
int var3 = integers.length;
int var4;
Integer integer;
for(var4 = 0; var4 < var3; ++var4) {
integer = var6[var4];
if (integer == 2) {
integer = 2222222;
}
}
var6 = integers;
var3 = integers.length;
for(var4 = 0; var4 < var3; ++var4) {
integer = var6[var4];
System.out.println(integer);
}
}
集合增强for的时候改变
@Test
public void test3(){
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(11);
list.add(111);
list.add(1111);
for (Integer integer : list) {
if(integer==11){
integer = 0;
}
}
for (Integer integer : list) {
System.out.println(integer);
}
}
结果
1
11
111
1111
增强for改变的.class文件
@Test
public void test3() {
ArrayList<Integer> list = new ArrayList();
list.add(1);
list.add(11);
list.add(111);
list.add(1111);
Iterator var2 = list.iterator();
Integer integer;
while(var2.hasNext()) {
integer = (Integer)var2.next();
if (integer == 11) {
integer = 0;
}
}
var2 = list.iterator();
while(var2.hasNext()) {
integer = (Integer)var2.next();
System.out.println(integer);
}
}
标签:integers,增强,var4,list,add,数组,Integer,集合,integer
From: https://www.cnblogs.com/Knightl8/p/16955042.html