在Java中,常见的遍历集合方式如下:

在Java、C#和C++中遍历集合(IList)Iterator iter = list.iterator();
}

也可以使用for

}


JDK 1.5引入的增强的for语法

在Java、C#和C++中遍历集合(IList)List list = 在Java、C#和C++中遍历集合(IList)
}

在C#中,遍历集合的方式如下:

在Java、C#和C++中遍历集合(IList)foreach (Object item in list) 
}

其实你还可以这样写,不过这样写的人很少而已

在Java、C#和C++中遍历集合(IList)IEnumerator e = list.GetEnumerator();
在Java、C#和C++中遍历集合(IList)
while (e.MoveNext()) 
}

在C# 2.0中,foreach能够作一定程度的编译期类型检查。例如:

在Java、C#和C++中遍历集合(IList)IList<int> intList = 在Java、C#和C++中遍历集合(IList)
编译出错


在C++标准库中。for_each是一种算法。定义如下:
for_each(InputIterator beg, InputIterator end, UnaryProc op)
C++中,由于能够重载运算符(),所以有一种特殊的对象,仿函数。

在Java、C#和C++中遍历集合(IList)template<class T>
));

相关文章: