美文网首页
内存分析、增强for循环

内存分析、增强for循环

作者: TheBestTheLost | 来源:发表于2019-10-29 20:34 被阅读0次
  • 图片.png
  • 增强for循环
    例子:
    package javatest;

import java.util.ArrayList;
import java.util.Collection;

public class EnhanceFor {
/* 增强for循环 */
public static void main(String[] args) {
int[] arr = { 1, 2, 3 };
for (int i : arr) {// 把数组里面的元素依次放入i中
System.out.println(i);
}
Collection<Object> c = new ArrayList<Object>();
c.add(new String("maliang"));
c.add(new String("sunjing"));
for (Object o : c) {// 把集合中的元素以此放入o中
System.out.println(o);
}
for (Object o : c) {// 把集合中的元素以此放入o中
System.out.println(o);
}
}
}

  • 增强for循环的弊端
    1.数组:不能方便的访问下标值
    2.集合:与使用iterator相比,不能方便的删除集合中的内容

相关文章

网友评论

      本文标题:内存分析、增强for循环

      本文链接:https://www.haomeiwen.com/subject/mjzgvctx.html