Java中普通for循环和增强for循环的一些区别

Java中for的几种常见形式

1.For loop using index.

for (int i = 0; i < arr.length; i++) { 
    type var = arr[i];
    body-of-loop
}
复制代码

2.Loop using explicit iterator.

for (Iterator<type> iter = coll.iterator(); iter.hasNext(); ) {
    type var = iter.next();
    body-of-loop
}
复制代码

3.Foreach loop over all elements in arr.

for (type var : coll) {
    body-of-loop
}
复制代码

For循环用来处理哪些数据结构

1.数组

int[] a = {1,2,3,4,5,6};
int[] b = new int[]{1,2,3,4,5,6};
int[] c = new int[6];

for (int i = 0; i < a.length; i++) {
    System.out.println(i);
}
for (int i : a) {
    System.out.println(i);
}
复制代码

2.实现了java.util.Iterator的类

import java.util.Iterator;

public class IterableTest<E> implements Iterable<E> {

    public static void main(String[] args) {
        IterableTest<Integer> integers = new IterableTest<Integer>();
        for (Integer integer : integers) {
            System.out.println(integer);
        }
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator() {

            @Override
            public boolean hasNext() {
                //...
                return false;
            }

            @Override
            public Object next() {
                //...
                return null;
            }

            @Override
            public void remove() {
                //...
            }
        };
    }
}
复制代码

普通for遍历和增强for的一些区别

增强的for循环的底层使用迭代器来实现,所以它就与普通for循环有一些差异

  1. 增强for使用增强for循环的时候不能使用集合删除集合中的元素;
  2. 增强for循环不能使用迭代器中的方法,例如remove()方法删除元素;
  3. 与普通for循环的区别:增强For循环有遍历对象,普通for循环没有遍历对象;

对于实现了RandomAccess接口的集合类,推荐使用普通for,这种方式faster than Iterator.next

The RandomAccess interface identifies that a particular java.util.List implementation has fast random access. (A more accurate name for the interface would have been "FastRandomAccess.") This interface tries to define an imprecise concept: what exactly is fast? The documentation provides a simple guide: if repeated access using the List.get( ) method is faster than repeated access using the Iterator.next( ) method, then the List has fast random access. The two types of access are shown in the following code examples.

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值