集合详细基础

本文详细介绍了Java中List接口的三种主要实现类:ArrayList、Vector和LinkedList。ArrayList基于数组实现,查询快但增删慢;Vector与ArrayList类似,但线程安全且效率较低;LinkedList采用链表结构,增删速度快但查询慢。通过示例代码展示了它们的使用方法和操作,包括添加、删除、遍历等操作,并分析了它们的内部结构和特点。

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

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

在编程语言中List是标准类库中的一个类,可以简单视之为双向链表,以线性列的方式管理物件集合。

提示:以下是本篇文章正文内容,下面案例可供参考

一、List实现类

  • ArrayList【重点】
    数组结构实现,查询快,增删慢;
    JDK1.2版本,运行效率快、线程不安全。

  • Vector

  • 数组结构实现,查询快、增删慢;

  • JDK1.0版本,运行效率慢、线程安全

  • LinkedList:

  • 链表结构实现,增删快,查询慢。

代码如下(示例):

package com.Collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/*
 *ArrayList的使用
 * 存储结构:数组 查找遍历速度快,增删慢
 */
public class ArrayList01 {
    public static void main(String[] args) {
        //创建集合
        ArrayList arrayList=new ArrayList<>();
        //添加元素
        Student s1=new Student("韩红",39);
        Student s2=new Student("汪涵",40);
        Student s3=new Student("何炅",50);
        Student s4=new Student("杨幂",20);

           arrayList.add(s1);
           arrayList.add(s2);
           arrayList.add(s3);
           arrayList.add(s4);
        System.out.println("元素个数: "+arrayList.size());
        System.out.println(arrayList.toString());
        //删除元素
        arrayList.remove(1);
       // arrayList.remove(s3);
        System.out.println("删除之后元素个数: "+arrayList.size());
        System.out.println(arrayList.toString());
        System.out.println("*---------------*");
       // arrayList.remove(new Student("杨幂",20));//equals(this==obj)
      //  System.out.println(arrayList.toString());
        //遍历元素
        //使用增强for
          for (Object object:arrayList){
              System.out.println(object);
          }
          //使用迭代器
        System.out.println("*-------------------*");
        Iterator it= arrayList.iterator();
          while (it.hasNext()){
              Student s=(Student) it.next();
              System.out.println(s.toString());
          }
          //使用列表迭代器
        System.out.println("*------------*");
        ListIterator lit=arrayList.listIterator();
          while (lit.hasNext()){
              Student s=(Student) lit.next();
              System.out.println(s.toString());
          }
          //逆序
        System.out.println("*-------逆序----------*");
        while (lit.hasPrevious()){
            Student s=(Student) lit.previous();
            System.out.println(s.toString());
        }
        //判断
        System.out.println(arrayList.contains(new Student("杨幂",20)));
        System.out.println(arrayList.isEmpty());
        //查找
        System.out.println(arrayList.indexOf(s3));
        System.out.println(arrayList.indexOf(new Student("杨幂",20)));
    }
}

package com.Collection;

public class Student {
    private String name;
    private int age;
     public Student(){

     }
    public Student(String name, int age) {
         super();
         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;
    }

    @Override
    public String toString() {
        return "Student [name=" + name +",age="+age+"]";
    }

    @Override
    public boolean equals(Object obj) {
         //判断是否是同一对象
         if (this==obj){
             return true;
         }
         //判断是否为空
         if (obj==null){
             return false;
         }
         //判断是否是Student类别
        if (obj instanceof Student){
            Student s=(Student) obj;
            //比较属性
            if (this.name.equals(s.getName())&&this.age==s.getAge()){
                return true;
            }
        }
        //不满足条件返回false
        return false;
    }
}

源码分析:DEFAULT_CAPACITY = 10; 默认容量
注意:如果没有向集合中添加任何元素时,容量为0
扩容是原来的1.5倍
elementData存放元素的数组
size 实际元素个数

二、Vector类

Vector类的使用

代码如下(示例):

package com.Collection;

import java.util.Enumeration;
import java.util.Vector;

public class Demo05 {
    public static void main(String[] args) {
        //创建集合
        Vector vector=new Vector();
        //添加元素
        vector.add("草莓");
        vector.add("芒果");
        vector.add("西瓜");
        vector.add("苹果");
        System.out.println("元素个数:"+vector.size());
        System.out.println(vector.toString());
        //删除
        //vector.remove("西瓜");
      //  vector.clear();
        //遍历元素
        //使用枚举器
        Enumeration en=vector.elements();
        while (en.hasMoreElements()){
            String o=(String)en.nextElement();
            System.out.println(o);
        }
        //判断
        System.out.println(vector.contains("西瓜"));
        System.out.println(vector.isEmpty());
        //其他方法
        System.out.println(vector.firstElement());//获取第一个元素
        System.out.println(vector.lastElement());//获取最后一个元素
        System.out.println(vector.elementAt(2));//获取数组下标为2的元素
    }
}

三、LinkedList

代码如下(示例):

public class Demo06 {
    public static void main(String[] args) {
        //创建集合
        LinkedList linkedList=new LinkedList<>();
        //添加元素
        Student s1=new Student("小红",39);
        Student s2=new Student("小兰",40);
        Student s3=new Student("小绿",50);
        Student s4=new Student("小紫",20);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        linkedList.add(s4);
        System.out.println("元素个数: "+linkedList.size());
        System.out.println(linkedList.toString());
        //删除元素
       // linkedList.remove(2);
       // linkedList.remove(new Student("小紫",20));
        System.out.println(linkedList.toString());
        //遍历元素
        System.out.println("*---------增强for----------*");
        for (Object object:linkedList){
            Student s=(Student)object;
            System.out.println(s);
        }
        System.out.println("*---------for遍历---------*");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        System.out.println("*-------使用迭代器----------*");
        Iterator it=linkedList.iterator();
        while (it.hasNext()){
            Student s=(Student) it.next();
            System.out.println(s.toString());
        }
        System.out.println("*-----------使用迭代器------*");
        ListIterator lit= linkedList.listIterator();
        while (lit.hasNext() ){
            Student s=(Student) lit.next();
            System.out.println(s.toString());
        }
        //判断
        System.out.println(linkedList.contains(s1));
        System.out.println(linkedList.isEmpty());
        //获取
        System.out.println(linkedList.indexOf(s2));
    }
}


源码分析:
int size:集合的大小
Node first:链表的头节点
Node last:链表的尾节点

下期预告

讲述泛型,其本质是参数化类型,把类型作为参数传递。
常见的形式有:泛型类、泛型接口、泛型方法
语法<T,…> T称为类型占位符,表示一种引用类型
好处:

  • 提高代码的重用性
  • 防止类型转换异常,提高代码的安全性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐雯.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值