Interator中中的remove()方法和foreach 循环遍历元素
注意点:’‘Tom’’.equals(obj)说明
如果interator里面存的是null
位置换一下就是空指针异常
常见错误说明
1.没有调next之前,iterator还在空指针,你删谁
1不能remve两次
代码运行结果
##foreach循环遍历元素
需要注意
foreach是吧集合对象中的元素赋值给局部变量,然后输出,并没有改变原来集合对象的值。
但如果用for循环复制的形式,会改变原来对象的值。
//手动实现的MyArrayList
class MyArrayList<T> implements Iterable<T>{
private T []elements;//用来作为存放元素的容器
private int size;//有效元素的个数
private static final int defaultCapacity=5;
public MyArrayList(){
this(defaultCapacity);
}
public MyArrayList(int capacity){
elements=