-集合进阶-

本文详细介绍了Java集合框架,包括Collection接口及其实现类ArrayList和LinkedList,List接口的特点和遍历方式,Set接口及其HashSet和TreeSet的区别,还有泛型的基本概念和应用场景,以及Map接口的使用和HashMap的遍历。文章通过实例展示了各种集合的使用方法,如存储和遍历学生对象,还探讨了并发修改异常和解决策略。最后,文章提及了Collections工具类的排序、反转和随机打乱列表元素的功能,并简单提及了一个斗地主游戏的实现。
摘要由CSDN通过智能技术生成

一、Collection集合

1.Collection集合概述和使用

概述:Collection集合是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素;JDK不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现

创建Collection集合的对象:多态的方式;具体的实现类ArrayList

public class HelloWorld {
    public static void main(String[] args)  {
        //创建Collection集合的对象
        Collection<String> c=new ArrayList<String>();
        
        //添加元素:boolean add(E e)
        c.add("hello");
        c.add("world");
        
        //输出集合对象
        System.out.println(c);
    }
}

2.Collection集合常用方法

方法名

说明

boolean add(E e) 添加元素
boolean remove(Object o) 从集合中移除指定的元素
void clear() 清空集合中的元素
boolean contains(Object o) 判断集合中是否存在指定的元素
boolean isEmpty() 判断集合是否为空
int size() 集合的长度,也就是集合中元素的个数

3.Collection集合的遍历

Iterator:迭代器,集合的专用遍历方式

Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到;迭代器是通过集合的iterator()方法得到的,所以说它是依赖于集合而存在的

常用方法 说明
E  next() 返回迭代中的下一个元素
boolean hasNext() 如果迭代具有更多元素,则返回true
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorDemo{
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
        Iterator<String> it=c.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

4.案例:Collection集合存储学生对象并遍历

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class HelloWorld {
    public static void main(String[] args){
        Collection<Student> c = new ArrayList<>();
        Student s1=new Student("x",18);
        Student s2 = new Student("y", 17);
        c.add(s1);
        c.add(s2);
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
            Student s=it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
public class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

二、List

1.List集合概述和特点

有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素;与Set集合不同,列表通常允许重复的元素。

有序:存储和取出的元素顺序一致

可重复:存储元素可重复

2.List集合的特有方法

方法名 说明
void add(int index,E element) 在此集合中的指定位置插入指定的元素
E remove(int index) 删除指定索引处的元素,返回被修改的元素
E set(int index,E element) 修改指定索引处的元素,返回被修改的元素
E get(int index) 返回指定索引处的元素

3.案例:List集合存储学生对象并遍历

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class HelloWorld {
    public static void main(String[] args){
        List<Student> list = new ArrayList<>();
        Student s1=new Student("x",18);
        Student s2 = new Student("y", 17);
        Student s3 = new Student("z", 13);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //迭代器
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            Student s=it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
        //for循环
        for(int i=0;i< list.size();i++){
            Student s = list.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

4.并发异常修改

并发修改异常:ConcurrentModificationException

产生原因:迭代器遍历的过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断预期修改值和实际修改值不一致

解决方案:用for循环遍历,然后用集合对象做对应的操作即可

5.ListIterator列表迭代器

通过LIst集合的listIterator()方法得到,所以说他是list集合特有的迭代器

用于允许程序猿沿任一方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置

方法 说明
E next() 返回迭代中的下一个元素
boolean hasNext() 如果迭代具有更多元素,则返回true
E previous() 返回迭代中的上一个元素
boolean hasprevious() 如果在相反方向迭代具有更多元素,则返回true
void add(E e) 将指定元素插入列表

6.增强for循环

简化数组和Collection集合的遍历

public class HelloWorld {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        for (int i : arr) {
            System.out.println(i);
        }
        System.out.println("------------");

        String[] strArray = {"hello", "world", "java"};
        for (String s : strArray) {
            System.out.println(s);
        }
        System.out.println("------------");

        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("java");
        for (String s : list) {
            System.out.println(s);
        }
    }
}

7.List集合存储学生对象并用三种方法遍历

public class HelloWorld {
    public static void main(String[] args) {
        List<Student> list=new ArrayList<Student>();
        Student s1 = new Student("x", 13);
        Student s2 = new Student("y", 11);
        Student s3 = new Student("z", 17);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //迭代器
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
            Student s = it.next
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值