JAVA | 53 - 类集框架 | 集合输出 |

本文详细介绍了Java中集合的四种输出方法:Iterator、ListIterator、foreach 和 Enumeration。通过具体示例展示了每种方法的特点及用法,包括如何遍历集合元素,并对比了不同迭代器之间的差异。

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

集合输出有四种方式:Iterator,ListIterator,Enumeration,foreach

集合输出绝大部分情况下都会使用 Iterator,Iterator 接口的实例化依靠 Collection 接口的 iterator 方法。

Iterator

import java.util.*;

class Book{
    private String title;
    private int price;
    public Book(String title, int price){
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return this.title + " " + this.price;
    }
    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Book other = (Book) obj;
        return Objects.equals(this.title, other.title)
                && Objects.equals(this.price, other.price);
    }
}
public class Main {
    public static void main(String[] args) throws Exception{
        // 设置了泛型,从而保证集合中所有的数据类型都一致
        List <Book> list = new ArrayList <Book>();
        list.add(new Book("java",100));
        list.add(new Book("c",89));
        list.add(new Book("c++",100));
        list.add(new Book("python",100));
        list.add(new Book("python",100));
        Iterator <Book> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

ListIterator

Iterator 接口的输出顺序为由前向后,Iterator 的子接口 ListIterator 输出顺序为由后向前。

ListIterator 子接口是专门为 List 子接口定义的输出接口。

ListIterator 想要实现由后向前的输出,则必须先实现由前向后的输出。

import java.util.*;

class Book{
    private String title;
    private int price;
    public Book(String title, int price){
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return this.title + " " + this.price;
    }
    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Book other = (Book) obj;
        return Objects.equals(this.title, other.title)
                && Objects.equals(this.price, other.price);
    }
}
public class Main {
    public static void main(String[] args) throws Exception{
        // 设置了泛型,从而保证集合中所有的数据类型都一致
        List <Book> list = new ArrayList <Book>();
        list.add(new Book("java",100));
        list.add(new Book("c",89));
        list.add(new Book("c++",100));
        list.add(new Book("python",100));
        list.add(new Book("python",100));
        ListIterator <Book> listIteratorator = list.listIterator();
        while (listIteratorator.hasNext()){
            System.out.println(listIteratorator.next());
        }
        System.out.println("----------");
        while (listIteratorator.hasPrevious()){
            System.out.println(listIteratorator.previous());
        }
    }
}

foreach

import java.util.*;

class Book{
    private String title;
    private int price;
    public Book(String title, int price){
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return this.title + " " + this.price;
    }
    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Book other = (Book) obj;
        return Objects.equals(this.title, other.title)
                && Objects.equals(this.price, other.price);
    }
}
public class Main {
    public static void main(String[] args) throws Exception{
        // 设置了泛型,从而保证集合中所有的数据类型都一致
        List <Book> list = new ArrayList <Book>();
        list.add(new Book("java",100));
        list.add(new Book("c",89));
        list.add(new Book("c++",100));
        list.add(new Book("python",100));
        list.add(new Book("python",100));
        for(Book book : list){
            System.out.println(book);
        }
    }
}

Enumeration

Enumeration 接口的实例化只能依靠 Vector 子类来实现。

import java.util.*;

class Book{
    private String title;
    private int price;
    public Book(String title, int price){
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return this.title + " " + this.price;
    }
    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Book other = (Book) obj;
        return Objects.equals(this.title, other.title)
                && Objects.equals(this.price, other.price);
    }
}
public class Main {
    public static void main(String[] args) throws Exception{
        // 设置了泛型,从而保证集合中所有的数据类型都一致
        Vector <Book> vector = new Vector <Book>();
        vector.add(new Book("java",100));
        vector.add(new Book("c",89));
        vector.add(new Book("c++",100));
        vector.add(new Book("python",100));
        vector.add(new Book("python",100));
        Enumeration <Book> enumeration = vector.elements();
        while (enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值