java用Iterator遍历容器r

这篇博客介绍了Java中使用Iterator遍历容器的基本操作,包括如何通过iterator()获取Iterator,使用next()和hasNext()方法移动和检查元素,以及如何使用remove()删除元素。示例代码展示了如何遍历Gerbil对象的List,并且提到了ArrayList的创建和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Iterator能够将遍历序列的操作与底层序列的结构分离,迭代器统一了对容器的访问方式。

package chapter11;


//import java.util.Iterator;
import java.util.*;
public class testIterator {
public static void display(Iterator<Gerbil> it) {
while(it.hasNext()) {
Gerbil g =it.next();
System.out.println(g.getid()+":"+g+" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Gerbil> g = Arrays.asList(new Gerbil("a"),new Gerbil("b"),new Gerbil("c"));
/*Gerbil a = new Gerbil("a");
Gerbil b = new Gerbil("b");
Gerbil c = new Gerbil("c");
g.add(a);
g.add(b);
g.add(c);*/
display(g.iterator());

}


}
class Gerbil{
private static int i;
private final int counter=i++;
private String name;
public int getid() {
return counter;
}
public Gerbil(String s) {
this.name=s;
}
public String toString() {
return name;
}
}

List<Gerbil> g = Arrays.asList(new Gerbil("a"),new Gerbil("b"),new Gerbil("c"));

这行代码用了Arrays类中的asList()方法返回了一个List对象。

也可以用另一种方法来实现

ArrayList<Rent> arr = new ArrayList<Rent>();
Rent x = new Rent("x",1);
Mouse y = new Mouse("y",2);
Gerbila z = new Gerbila("z",3);

arr.add(x);
arr.add(y);
arr.add(z);

先新建一个ArrayList 在ArrayList中add三个对象

代码中的display()方法不包含任何有关它遍历的序列的类型信息

Java中的Iterator功能比较简单,并且只能单向移动:

  (1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。

  (2) 使用next()获得序列中的下一个元素。

  (3) 使用hasNext()检查序列中是否还有元素。

  (4) 使用remove()将迭代器新返回的元素删除。

JAVA编程思想155页例题

用iterator来变历访问Rent

package chapter11;
import java.util.*;
public class testIteratorRent {
public static void display(Iterator<Rent> it) {
while(it.hasNext()) {
Rent e = it.next();
System.out.print(e+" ");
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Rent> arr = new ArrayList<Rent>();
Rent x = new Rent("x",1);
Mouse y = new Mouse("y",2);
Gerbila z = new Gerbila("z",3);

arr.add(x);
arr.add(y);
arr.add(z);

display(arr.iterator());
}


}
class Rent{
private String name;
private int age;
public Rent(String na,int in) {
setName(na);
setAge(in);
}
public String toString() {
return name+" "+age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String s) {
this.name=s;
}
public void setAge(int i) {
this.age=i;
}
}
class Mouse extends Rent{
//private String name;
//private int age;
public Mouse(String s,int i) {
super(s,i);
}
public String toString() {
return getName()+" "+getAge();
}
}
class Gerbila extends Rent{
public Gerbila(String n,int i){
super(n,i);
}
public String toString() {
return getName()+" "+getAge();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值