基于JDK5.0一些collection类的使用总结
在5.0中,collection最大的一个改变就是可以指定它的具体类型: h(l*b sy|*e(Vr6c0Yj
List list=new List; U |;X?L5M1Q;XK
9k&ST_.y7Y'B
,TcHk3L
两个最基本的接口:
public interface Collection 2d,g2jU6}6PS
8Z7F.sW&NH
{ 6{6Su[;Jj_
boolean add(E element); 4vOJT!c,_,Kd
5vHH`r.pM
Iterator iterator(); %zp'B I Bu1u
. . . #`5Dp3F8_ e*u
Q'E9J;fv+Rv
} uNb5`TLV_mc.D
8Vc(}eN3h
Jt;i'I#~*aumj
public interface Iterator Xe;qt3g#\0|
0s?;M){p
{ #KN _X o9@:c+g4@~
E next();
3jb5QT)V
boolean hasNext(); &Z:Gb$`6f
void remove();
}
在5.0以前,常用的形式就是: k:u6n ^2X j @
Collection c = . . .;
u8Tat+yX
Iterator iter = c.iterator();
:f4X#D @G1H
while (iter.hasNext())
{
String element = iter.next(); [:BL@rx;[
%Kj ]2BXg |
do something with element
} *bI/`1P+r^|'_
0pS/q3{,r6i f2o#o
但是在5.0中加入另外一种循环方式,类似于for each: )fkJNXtQ!O
;d:O(t:R0G1`4k)o$[;X
for (String element : c)
(z6fs6L | [
{
do something with element
3K0IM5J2^0U
} l z_{]s!E