美文网首页Java 杂谈Java互联网科技
深入浅析Java集合及LIst接口

深入浅析Java集合及LIst接口

作者: cd4bd3aa39ec | 来源:发表于2019-05-24 21:55 被阅读4次

    一、集合的概念
    1.概述:

    在学习集合前,先回忆一下数组的一个特征---数组有固定的长度,定义一个数组:int[] array = new int[];而针对数据长度可变的情况,产生了集合,java集合就是为了应对动态增长数据,在编译时无法知道具体的数据量而产生的。
    集合类又叫容器类。

    2.集合和数组的区别

    都是容器,数组时固定的长度,集合时可变的;
    数组存放的数据都是基本数据类型(四类八种)集合存放的数据都是引用数据类型(String、Integer、自定义数据类型)
    集合中对于基本数据类型会转换位引用数据类型再存储。
    3.集合包含内容、集合的框架

    接口:Collection,Map,Set,List等(其中Set和List继承了Collection)
    抽象类:AbstractCollection,AbstractList等(实现了部分方法)
    实现类:ArrayList,LinkedList,HashMap等
    迭代器:Iterator(集合的访问迭代,返回集合中的元素的迭代器)
    二、List集合
    1.概述

    List集合是一个有序的、可重复的集合,集合中每一个元素都有对应的顺序索引。

    List允许加入重复元素是应为可以通过索引来访问指定位置的元素。

    List集合默认按照元素的添加顺序增加元素的索引。

    2.ArrayList

    1>概述

    ArrayList是基于数组实现的List类,实现所有可选列表操作,允许所有元素包括null

    2>初始化

    ArrayList arrayList = new ArrayList(); =>初始容量为10的列表集合

    ArrayList<E> arrayList = new ArrayList<E>(); =>数据类型为E,初始容量为10

    3>主要方法
    boolean add(E e) -->将指定的元素追加到此列表的末尾。

    void add(int index, E element) -->在此列表中的指定位置插入指定的元素。

    boolean addAll(Collection<? extends E> c) -->按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。

    boolean addAll(int index, Collection<? extends E> c) -->将指定集合中的所有元素插入到此列表中,从指定的位置开始。

    boolean contains(Object o) -->如果此列表包含指定的元素,则返回 true 。

    E get(int index) -->返回此列表中指定位置的元素。

    E remove(int index) -->删除该列表中指定位置的元素。

    E set(int index, E element) -->用指定的元素替换此列表中指定位置的元素。

    Object[] toArray() -->以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。

    /**
     * @ author: PrincessHug
     * @ date: 2019/2/10, 0:18
     * @ Blog: https://www.cnblogs.com/HelloBigTable/
     */
    public class ArrayListDemo01 {
     public static void main(String[] args) {
     ArrayList<String> arr = new ArrayList<String>();
     arr.add("123");
     System.out.println(arr);
     ArrayList<Person> person = new ArrayList<Person>();
     Person p1 = new Person("Wyh",18);
     Person p2 = new Person("Hunter", 40);
     person.add(p1);
     person.add(p2);
     for (int i=0;i<person.size();i++) {
     System.out.println(person.get(i));
     }
     System.out.println(person.contains(p2));
     person.remove(1);
     person.set(0,p2);
     Person[] persons = new Person[2];
     person.toArray(persons);
     System.out.println(persons[0]);
     System.out.println(persons[1]);
     }
    }
      
    public class Person {
     private String name;
     private int age;
      
     public Person(){}
      
     public Person(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;
     }
      
     @Override
     public String toString() {
     return "["+this.name+","+this.age+"]";
     }
    }
    

    3.List集合遍历的4种方法

    通过List.size()方法作为for循环的条件,同数组遍历
    通过迭代器Iterator  Iterator<Integer> it = arrayList.iterator(); while循环,hasNext作为判断条件,next()获取集合元素再输出。
    增强for循环
    jdk1.8新特性foreach

    /**
     * @ author: PrincessHug
     * @ date: 2019/2/12, 2:43
     * @ Blog: https://www.cnblogs.com/HelloBigTable/
     */
    public class PrintArrayList {
      public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Student s1 = new Student("001", "Wyh", '男');
        Student s2 = new Student("002", "Fyh", '男');
        Student s3 = new Student("003", "Zxy", '男');
        students.add(s1);
        students.add(s2);
        students.add(s3);
      
        System.out.println("通过size()方法作为for循环条件遍历:");
        for (int i=0;i<students.size();i++){
          System.out.println(students.get(i));
        }
      
        System.out.println("通过迭代器遍历集合:");
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
          System.out.print(iterator.next() + "\t");
        }
      
        System.out.println("通过增强for循环遍历集合:");
        for (Student stu:students){
          System.out.println(stu);
        }
        System.out.println("通过jdk1.8新特性forEach遍历集合:");
        students.forEach(student -> System.out.println(student));
      }
    }
    

    4.LinkedList

    1.概述:LinkedList指的是链表类的数据结构

    2.LinkedList与ArrayList的区别:

    a)链表中的元素可以任意的增加和删除,但是查询效率不如列表
    b)链表将对象存放在独立的空间中,而且每个空姐保存了下一个链接的索引
    构造方法 LinkedList<E> linkedList = new LinkedList<E>();

    3.主要方法

    void addFirst(E e) -->在该列表开头插入指定的元素。
    void addLast(E e) -->将指定的元素追加到此列表的末尾。
    E peekFirst() -->检索但不删除此列表的第一个元素,如果此列表为空,则返回 null 。
    E peekLast() -->检索但不删除此列表的最后一个元素,如果此列表为空,则返回 null 。
    E pollFirst() -->检索并删除此列表的第一个元素,如果此列表为空,则返回 null 。
    E pop() -->从此列表表示的堆栈中弹出第一个元素。相似于removeFirst()
    void push(E e) -->将元素推送到由此列表表示的堆栈上。相似于addFirst()

    /**
     * @ author: PrincessHug
     * @ date: 2019/2/10, 2:12
     * @ Blog: https://www.cnblogs.com/HelloBigTable/
     */
    public class LinkedListDemo {
     public static void main(String[] args) {
     LinkedList<String> linkedList = new LinkedList<>();
     linkedList.addFirst("is");
     linkedList.addFirst("Wyh");
     linkedList.addLast("cool");
     System.out.println(linkedList);
     System.out.println(linkedList.peekFirst());
     System.out.println(linkedList.pollFirst());
     System.out.println(linkedList);
     System.out.println(linkedList.pop());
     System.out.println(linkedList);
     linkedList.push("Wyh is");
     System.out.println(linkedList);
     }
    }
    

    以上所述是小编给大家介绍的Java集合及LIst接口详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。

    相关文章

      网友评论

        本文标题:深入浅析Java集合及LIst接口

        本文链接:https://www.haomeiwen.com/subject/lcznzqtx.html