数据结构

栈:先进后出
队列:先进先出
数组:
查询快:数组的地址是连续的,我们通过数组的首地址可以找到数组,通过数组的索引可以快速查找某一元素。
增删慢:数组的长度是固定的,我们想要增加/删除一个元素,必须创建一个新数组,把原数组的数据复制过来,把新数组的地址赋值变量,原数组在内存中被销毁(在堆中)。

链表:
查询慢:链表中的地址不是连续的,都必须从头开始查询
增删快:链表结构,增加/删除一个元素,对链表的整体结构没有影响,所以快。

单向链表:链表中只有一条链,不能保证元素的顺序。
双向链表:链表中有两条链,有一条值专门记录元素的顺序,是一个有序的集合。

List集合

java.util.list接口 extends Collection接口
list接口的特点:
1、有序的集合,存储元素和取出元素的顺序是一致的(存储123,取出123)
2、有索引,包含了一些带索引的方法
3、允许存储重复元素

List接口中带索引的方法:

  • public void add(int index, E element):将指定的元素,添加到该集合中的指定位置上。

  • public E get(int index):返回集合中指定位置的元素。

  • public E remove(int index): 移除列表中指定位置的元素, 返回的是被移除的元素。

  • public E set(int index, E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
    注意:
    操作索引的时候,一定要防止索引越界异常
    IndexOutOfBoundsException:索引越界异常,集合会报
    ArrayIndexOfBoundException:数组索引越界异常
    StringIndexOfBoundException:字符串索引越界异常

    public class Demo01List {
        public static void main(String[] args) {
    
            //创建一个List集合对象,多态
            List<String> list = new ArrayList<>();
            //使用add方法往集合中添加元素
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("a");
            //打印集合
            System.out.println(list);//[a,b,c,d,a]不是地址,重写了toString
            // public void add(int index, E element)`: 将指定的元素,添加到该集合中的指定位置上。
            //在c和d之间添加一个itheima
            list.add(3,"itheima");
     		System.out.println(list);//[a, b, c, itheima, d, a]
            //public E remove(int index)`: 移除列表中指定位置的元素, 返回的是被移除的元素。
            //移除c
            String remove = list.remove(2);
            System.out.println("被移除的元素是:"+remove);
            System.out.println(list);//[a, b,itheima, d, a]
    
            //public E set(int index, E element)`:用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
            //把最后一个a替换成A
            String a = list.set(4, "A");
            System.out.println("被替换的元素:"+a);
            System.out.println(list);//[a, b, itheima, d, A]
    
            //list集合遍历有三种方式
            //1、使用普通的for循环遍历
            for (int i = 0; i <list.size() ; i++) {
                String b=list.get(i);
                System.out.println(b);
            }
            System.out.println("======================");
            //使用迭代器循环
            Iterator<String> iterator = list.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                System.out.println(next);
            }
            System.out.println("=======================");
            //使用增强for循环遍历
            for (String c:list
                 ) {
                System.out.println(c);
            }
            //String s = list.get(5);IndexOutOfBoundsException索引越界异常
        }
    }
    

LinkedList集合

java.util.linkedList集合 implements List接口(List接口的链接列表实现)
linkedList集合的特点:
1.底层是一个链表结构:查询慢,增删快
2.里面包含了大量操作收尾元素的方法
注意:使用LinkList集合特有的方法,不能使用多态

  • public void addFirst(E e):将指定元素插入此列表的开头。

  • public void addLast(E e):将指定元素添加到此列表的结尾。

  • public void push(E e):将元素推入此列表所表示的堆栈。

  • public E getFirst():返回此列表的第一个元素。

  • public E getLast():返回此列表的最后一个元素。

  • public E removeFirst():移除并返回此列表的第一个元素。

  • public E removeLast():移除并返回此列表的最后一个元素。

  • public E pop():从此列表所表示的堆栈处弹出一个元素。

  • public boolean isEmpty():如果列表不包含元素,则返回true

public class Demo01LinkedList {
    public static void main(String[] args) {
        show01();
        System.out.println("=====================");
        show02();
        System.out.println("=======================");
        show03();
    }
     /*`public E removeFirst()`:移除并返回此列表的第一个元素。
            * `public E removeLast()`:移除并返回此列表的最后一个元素。
            * `public E pop()`:从此列表所表示的堆栈处弹出一个元素。*/
    private static void show03() {
        //创建linkedList集合对象
        LinkedList<String> linked = new LinkedList<>();
        //使用add方法往集合中添加元素
        linked.add("a");
        linked.add("b");[a, b, c]

        linked.add("c");
        System.out.println(linked);//[a, b, c]
        String s = linked.removeFirst();//同linked.pop
        System.out.println("被移除的第一个元素:"+s);
        String s1 = linked.removeLast();//被移除的第一个元素:a
        System.out.println("被移除的最后一个元素"+s1);
        System.out.println(linked);//被移除的最后一个元素c

    }

    /*public E getFirst()`:返回此列表的第一个元素。
     * `public E getLast()`:返回此列表的最后一个元素。*/
    private static void show02() {

        //创建linkedList集合对象
        LinkedList<String> linked = new LinkedList<>();
        //使用add方法往集合中添加元素
        linked.add("a");
        linked.add("b");
        linked.add("c");
        String first = linked.getFirst();
        String last = linked.getLast();
        System.out.println(first);//a
        System.out.println(last); //c
    }

    /*
 * `public void addFirst(E e)`:将指定元素插入此列表的开头。
 * `public void addLast(E e)`:将指定元素添加到此列表的结尾。
 * `public void push(E e)`:将元素推入此列表所表示的堆栈。

 */
    private static void show01() {
        //创建linkedList集合对象
        LinkedList<String> linked = new LinkedList<>();
        //使用add方法往集合中添加元素
        linked.add("a");
        linked.add("b");
        linked.add("c");
        System.out.println(linked);//[a, b, c]

        // * `public void addFirst(E e)`:将指定元素插入此列表的开头。
        //linked.addFirst("www");
        //System.out.println(linked);//[www, a, b, c]
        linked.push("www");
        System.out.println(linked);//[www, a, b, c]同addFirst

        //public void addLast(E e)`:将指定元素添加到此列表的结尾。
        linked.addLast("com");
        System.out.println(linked);

    }
}

HashSet集合

java.util.Set接口 extends Collection接口
Set接口的特点:
1.不允许存储重复的元素
2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历
java.util.HashSet集合 implements Set接口
HashSet特点:
1.不允许存储重复元素
2.没有索引,没有带索引的方法,也不能使用普通for循环
3.是一个无序的集合,存储元素和取出元素的顺序有可能不一致
4.底层是一个哈希表结构(查询的速度非常快)

public class Demo01Set {
    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        //使用add方法往集合中添加元素
        set.add(1);
        set.add(3);
        set.add(2);
        set.add(1);
        //使用迭代器遍历set集合
        Iterator<Integer> it = set.iterator();
        while (it.hasNext()){
            Integer n = it.next();
            System.out.print(n);//1,2,3
        }
        //使用增强for遍历Set集合
        System.out.println("=====================");
        for (Integer integer : set) {
            System.out.println(integer);//1,2,3
        }
    }
}

哈希值:是一个十进制整数,由系统随机给出(一个模拟出来的逻辑地址)。
在Object类有一个方法,可以获取对象的哈希值。
int hashCode()但会该对象的哈希码值。
hashCode方法的源码:
public native inthashCode();
native:代表该方法调用的是本地操作系统的方法。

哈希表:数组+链表/红黑树

哈希表特点:查询速度快。存储数据到集合中,先计算元素的哈希值。再在哈希值相同的元素中查找,如果链表(元素)的长度超过8位,那么链表就会转换为红黑树,提高查询速度。

Set集合不允许存储重复元素的原理:Set集合在调用add方法的时候,add方法会调用元素的hashCode方法和equals方法,判断元素是否重复。
前提:存储的元素必须重写hashCode方法和equals方法

public class HashSetSaveString {
    public static void main(String[] args) {
        //创建HashSet集合对象
        HashSet<String> set = new HashSet<>();
        String s1=new String("abc");
        String s2=new String("abc");
        set.add("重地");
        set.add("通话");
        set.add("abc");
        System.out.println(set);//[重地, 通话, abc]
    }
}

LinkedHashSet

java.util.linkedHashSet集合 extends HashSet集合
LinkedHashSet集合特点:
底层是一个哈希表(数组+链表/红黑树)+链表:多了一条链表,记录元素的存储顺序,保证有序。

import java.util.HashSet;
import java.util.LinkedHashSet;

public class Demo01LinkedHashSet {
    public static void main(String[] args) {
        HashSet<String> set=new HashSet<>();
        set.add("www");
        set.add("abc");
        set.add("www");
        set.add("itcast");
        System.out.println(set);//[abc, www, itcast],无序,不允许重复

        LinkedHashSet<String> linked = new LinkedHashSet<>();
                linked.add("www");
                linked.add("abc");
                linked.add("www");
                linked.add("itcast");
        System.out.println(linked);//[www, abc, itcast]、有序,不允许重复
    }
}

可变参数

可变参数:是JDK1.5之后出现的新特征
使用前提:当方法的参数列表数据类型已经确定,但是参数的个数不确定,就可以使用可变参数。
使用格式:定义方法时使用: 修饰符 返回值类型 方法名(数据类型...变量名){}
可变参数的原理:可变参数底层就是一个数组,根据传递参数个数不同,会创建不同长度的数组,来存 储这些参数传递的参数的个数,可以是0个(不传递),1,2...多个

public class Demo01 {
    public static void main(String[] args) {
        int i=add();
        int a=add(1,2,3,4);
        System.out.println(a);//10
        System.out.println(i);//0

        method("abc",3.1,1);

        }
        /*
    定义计算(0-n)个整数和的方法
    已知:计算整数的和,数据类型已经确定int
    但是参数的个数不确定,不知道要计算几个整数的和,就可以使用可变参数
    add():就会创建一个长度为0的数组,new int[0]
    add(10);就会创建一个长度为1的数组,存储传递过来的参数new int[]{10}
    add(10,20);就会创建一个长度为2的数组,存储传递过来的参数new int[]{10,20}

            注意:
                1.一个方法的参数列表,只能有一个可变参数
                2.如果方法的参数有多个,那么可变参数必须写在参数列表的末尾

         */
            //public static void method(int...a,String...b){}只能有一个可变参数
   //如果方法的参数有多个,那么可变参数必须写在参数列表的末尾
    public static void method(String b,double c,int...a){

        }
        //可变参数的特殊写法
    //定义一个方法,计算两个int类型整数的和
    public static void method1(Object...obj){

    }
    public static int add(int...arr)  {
        //System.out.println(arr);//[I@5fd0d5ae底层是一个数组
        //System.out.println(arr.length);//0
        //定义一个初始化的变量,记录累加求和
        int sum=0;
        //遍历数组,获取数组中的每一个元素
        for (int a:arr
             ) {
            sum=sum+a;
        }
        return sum;
    }
}