栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

集合详细基础

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

集合详细基础

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

文章目录
  • 前言
    • 在编程语言中List是标准类库中的一个类,可以简单视之为双向链表,以线性列的方式管理物件集合。
  • 一、List实现类
  • 二、Vector类
      • Vector类的使用
  • 三、LinkedList
  • 下期预告


前言

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

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

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

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

  • Vector

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

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

  • LinkedList:

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

代码如下(示例):

package com.Collection;

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


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称为类型占位符,表示一种引用类型
好处:

  • 提高代码的重用性
  • 防止类型转换异常,提高代码的安全性
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号