java集合框架复习----(2)List

三、List集合

特点

  • 有序,打印输出的顺序和添加时的顺序一致(不会帮你自动排序)
  • 有下标,可以通过下标的形式访问对象
  • 元素可以重复

方法

  • void add(int index,Object o) //在index位置插入元素
  • boolean addAll(int index,Collection c) //将一个集合中的元素添加到次集合中的index位置
  • Object get(int index) //返回集合中指定位置的元素
  • List subList(int fromIndex,int toIndex) //返回二者之间的元素
package com.zheng.demo1;

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

public class MyList2 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(2);
        list.add(3);
        list.add(1);
        list.add(5);
        list.add(2);
        list.add(4);

        System.out.println("通过下标访问:" + list.get(0));

        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }
    }
}

在这里插入图片描述

listIterator:迭代器

hasNext(); 查看是否有下一个元素。有为true,无为false

  • next(); 取出下一个元素的值
  • hasPrevious();查看前一个元素是
  • previous();取出前一个元素
  • remove(); 删除当前元素
package com.zheng.demo1;

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

public class MyList {
    public static void main(String[] args) {
        //创建一个list集合容器
        List list = new ArrayList();

        //添加数据
        list.add("1-小红");
        list.add("2-小黑");
        list.add("3-小青");
        list.add("4-小紫");

        System.out.println("当前集合大小" + list.size());

        //一般的遍历
        System.out.println("=========一般的遍历=======");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));

        }
        System.out.println("=========增强for循环遍历=======");
        //增强for循环遍历
        for (Object o : list) {
            System.out.println(o);

        }

        System.out.println("=========一般的迭代器遍历=======");
        //一般的迭代器遍历
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        System.out.println("=========list迭代器从前到后遍历=======");
        //list特有的
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            String str = (String) listIterator.next();
            System.out.println(str);
        }

        System.out.println("=========list迭代器从后到前遍历=======");
        while (listIterator.hasPrevious()) {
            Object obj = listIterator.previous();
            System.out.println(obj);
        }

    }
}

在这里插入图片描述

List实现类

1、ArrayList【重点】

  • 数据结构实现,查询快(数组是连续的存储地址)。增删慢(每增加或者删除都要移动大量的数据。末尾快一点)
  • jdk1.2版本。运行效率高,线程不安全

2、Vector:

  • 数组结构,查询快,增删慢
  • jdk1.0版本,运行效率低,线程安全

3、LinkedList

  • 链表结构实现,增删快(不连续的存储空间,只需要改变节点之间的指向)查询慢
package com.zheng.demo1;

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

public class MyLinkList {

    public void print(LinkedList<Student> students) {
        //迭代器遍历
        System.out.println("======迭代器遍历======");
        ListIterator<Student> studentListIterator = students.listIterator();
        while (studentListIterator.hasNext()) {
            Student student = studentListIterator.next();
            System.out.println(student);
        }
    }

    public static void main(String[] args) {
        MyLinkList myLinkList = new MyLinkList();

        //1、创建一个容器
        LinkedList<Student> students = new LinkedList<>();

        //2、创建对象
        Student student1 = new Student(1, "小黑", "周口");
        Student student2 = new Student(2, "小名", "沈丘");
        Student student3 = new Student(3, "小郑", "范营乡");
        Student student4 = new Student(4, "小芳", "666");

        //3、将对象加入集合
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);

        //查看
        System.out.println(students.toString());
        
        //调用迭代器遍历
        myLinkList.print(students);

        //删除
        System.out.println("=====删除某个元素=====");
        students.remove(1);
        myLinkList.print(students);


    }
}

在这里插入图片描述
四、泛型
jdk1.5中引入的新特性。本质是参数化类型,把类型当做参数传递

  • 泛型接口、泛型方法、泛型类
  • <T,…> T为占位符,表示一种引用类型
  • 优点:(1)、提高代码的复用。(2)、防止类型转换异常,提高代码的安全性

1、泛型类

泛型变量

  • 泛型作为传递的参数
  • 泛型作为方法的返回值

注意:

  • 泛型只能使用引用类型
  • 不同泛型类型对象之间不能相互赋值
package com.zheng.demo2;

//泛型类
public class MyGeneric<T> {

    T t;//1、泛型变量

    //2、泛型作为传递的参数
    public void show(T t) {
        System.out.println(t);
    }

    //3、泛型作为方法的返回值
    public T getT() {
        return t;
    }
}

package com.zheng.demo2;

public class TestMyGeneric {

    public static void main(String[] args) {
        //使用泛型类创建对象
        MyGeneric<String> sm = new MyGeneric<>();
        sm.t = "你好";
        sm.show("呦呦呦");
        System.out.println(sm.getT());

        System.out.println("=============");

        MyGeneric<Integer> im = new MyGeneric<>();
        im.t = 999;
        im.show(666);
        System.out.println(im.getT());


    }
}

在这里插入图片描述

2、泛型接口

package com.zheng.demo2;

public interface MyInterface<T> {
    T show(T t);
}

实现类

package com.zheng.demo2;

public class MyInterfaceImpl1 implements MyInterface<Integer>{

    @Override
    public Integer show(Integer integer) {
        return integer;
    }

    public static void main(String[] args) {
        MyInterfaceImpl1 impl1 = new MyInterfaceImpl1();
        System.out.println(impl1.show(999));
    }


}

在这里插入图片描述
2、

package com.zheng.demo2;

public class MyInterfacetImpl implements MyInterface<String>{
    public String show(String s) {
        return s;
    }

    public static void main(String[] args) {
        MyInterface omf = new MyInterfacetImpl();
        System.out.println(omf.show("hhhhhh"));
    }
}

在这里插入图片描述
3、

package com.zheng.demo2;

public class MyInterfaceImpl2<T> implements MyInterface<T> {

    @Override
    public T show(T t) {
        return t;
    }

    public static void main(String[] args) {
        MyInterfaceImpl2<String> impl2 = new MyInterfaceImpl2<>();
        System.out.println(impl2.show("daoajao"));
        System.out.println("============");
        MyInterfaceImpl2<Double> impl21 = new MyInterfaceImpl2<>();
        System.out.println(impl21.show(520.1313));
    }
}

在这里插入图片描述
3、泛型方法

package com.zheng.demo2;

public class MyGenericMethod {

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


    public static void main(String[] args) {
        System.out.println(new MyGenericMethod().show(5));
        System.out.println("=============");
        System.out.println(new MyGenericMethod().show("哈哈哈哈"));
    }
}

在这里插入图片描述

posted on 2022-08-28 22:19  热爱技术的小郑  阅读(13)  评论(0编辑  收藏  举报