Day 20 集合框架(2)

本文详细介绍了Java中的Vector和LinkedList两种集合框架,分析了它们的内部实现及优缺点。Vector基于数组实现,查询快但增删操作效率较低,且线程安全;LinkedList采用链表结构,增删速度快但查询慢。此外,还探讨了Java泛型的使用,包括泛型类、泛型接口和泛型方法,并展示了泛型在集合中的应用,强调了泛型提高代码安全性与重用性的优点。
摘要由CSDN通过智能技术生成

集合框架(2)

Vector

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

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

package United;

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

public class Demo06 {
    public static void main(String[] args) {
        //创建数组
        Vector a = new Vector();
        //1. 添加元素
        a.add("华为");
        a.add("苹果");
        a.add("三星");
        System.out.println("元素个数:"+a.size());
        System.out.println(a.toString());
        //2. 删除
        //a.remove(0);
        //a.remove("三星");
        //a.clear();
        //3. 遍历
        //使用枚举器  了解即可
        Enumeration b = a.elements();
        while (b.hasMoreElements()){
            String c = (String)b.nextElement();
            System.out.println(c);
        }
        //4. 判断
        System.out.println(a.contains("华为"));
        System.out.println(a.isEmpty());
        //5. Vector其他方法
        //firstElement  lastElement  elementAt
    }
}

LinkedList

链表结构实现,增删快,查询慢 双向链表结构

package United;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;


public class Demo07 {
    public static void main(String[] args) {
        //创建集合
        LinkedList a = new LinkedList();
        //1. 添加元素
        a.add("华为");
        a.add("魅族");
        a.add("小米");
        System.out.println("元素个数:"+a.size());
        System.out.println(a);
        //2. 删除元素
        //a.remove(0);
        //a.remove("华为");
        //a.clear();
        //3.1 遍历  for循环
        for (int i = 0; i < a.size(); i++) {
            System.out.println(a.get(i));
        }
        System.out.println("=====================");
        //3.2 增强for循环
        for (Object b:a) {
            System.out.println(b);
        }
        System.out.println("=====================");
        //3.3 迭代器
        Iterator c = a.iterator();
        while (c.hasNext()){
            System.out.println(c.next());
        }
        System.out.println("=====================");
        ListIterator d = a.listIterator();
        while (d.hasNext()){
            System.out.println(d.next());
        }
        //4. 判断
        System.out.println(a.contains("华为"));
        System.out.println(a.isEmpty());
        //5. 获取
        System.out.println(a.indexOf("华为"));
    }
}

LinkedList源码分析

    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

ArrayList和LinkedList的区别

在这里插入图片描述

泛型

Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。

常见形式有泛型类、泛型接口、泛型方法。

语法:

  • <T,…> T称为类型占位符,表示一种引用类型。

好处:

  • (1)提高代码的重用性
  • (2)防止类型转换异常,提高代码的安全性

泛型类

package United;

public class MyGeneric<T> {//T是类型占位符,表示一种引用类型,如果编写多个用逗号隔开
    //使用泛型
    //使用过程中不能实例化泛型,原因是不知道泛型T是什么类型的变量,不知道其方法公有还是私有
    //1. 创建变量
    T t;
    //2. 泛型作为方法的参数
    public void show(T t){
        System.out.println(t);
    }
    //3. 泛型作为方法的返回值
    public T getT(){
        return t;
    }
}

package United;

public class TestGeneric {
    public static void main(String[] args) {
        //使用泛型类创建对象
        //注意:1. 泛型只能使用引用类型   2. 不同泛型对象之间不能相互赋值
        MyGeneric<String> myGeneric = new MyGeneric<>();
        myGeneric.t="这是传递参数";
        myGeneric.show("这是调用方法中的传递参数");
        String a = myGeneric.getT();

        MyGeneric<Integer> myGeneric2 = new MyGeneric<>();
        myGeneric2.t=20;
        myGeneric2.show(100);
        Integer b = myGeneric2.getT();
    }
}

泛型接口

package United;

//这是在写接口时确定好了实现的类型,在实例化时就不能更改了
public class MyInterfaceImpl implements MyInterface<String>{
    @Override
    public String serve(String s) {
        System.out.println(s);
        return s;
    }
}

package United;

//这是将泛型接口”套在“类上,使类也成为泛型,类型就不确定了,在实例化时可以更改自己需要的类型
public class MyInterfaceImpl02<T> implements MyInterface<T>{
    @Override
    public T serve(T t) {
        System.out.println(t);
        return t;
    }
}

public static void main(String[] args){
    	//法一
        MyInterfaceImpl c = new MyInterfaceImpl();
        c.serve("hello");

    	//法二
        MyInterfaceImpl02<Integer> d = new MyInterfaceImpl02<>();
        d.serve(20);
}

泛型方法

package United;

public class MyGenericMethod {
    public <T>T show(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}

public static void main(String[] args){
        MyGenericMethod e = new MyGenericMethod();
        e.show("华为");
        e.show(100);
        e.show(3.14);
}

泛型集合

概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致。

特点:

  • 编译时即可检查,而非运行时抛出异常。
  • 访问时,不必类型转换(拆箱)。
  • 不同泛型之间引用不能相互赋值,泛型不存在多态。
package United;

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

//泛型集合
public class MyGeneric02 {
    public static void main(String[] args) {
        ArrayList<String> a = new ArrayList();
        a.add("黄淮");
        a.add("杨花");
        //a.add(100);
        //a.add(50);

        for (String s: a) {
            System.out.println(s);
        }

        ArrayList<Student> b = new ArrayList<>();
        Student s1 = new Student("刘德华",45);
        Student s2 = new Student("古天乐",43);
        Student s3 = new Student("梁朝伟",47);
        b.add(s1);
        b.add(s2);
        b.add(s3);

        Iterator<Student> iterator = b.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

好好学习争取保研

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

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

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

打赏作者

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

抵扣说明:

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

余额充值