2021-11-16数据结构
生活随笔
收集整理的這篇文章主要介紹了
2021-11-16数据结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.棧和隊列
2.數組
3.鏈表
雙向鏈表既能指向下一個元素,也能指向上一個元素。
4.紅黑樹
查詢速度非常快。
5、List集合
list的特點:
(1)有序的集合,存儲元素和取出元素的順序是一致的(存儲123,取出123)
(2)有索引,包含了一些帶索引的方法
(3)允許存儲重復的元素。
java.util.list接口 extends Collection接口
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):用指定元素替換集合中指定位置的元素,返回值是被替換的元素。
注意:
操作索引的時候,一定要防止索引越界異常
IndexOutOfBoundException:索引越界異常,集合會報
ArrayIndexOutOfBoundException:數組索引越界異常
StringIndexOutOfBoundsException:字符串索引越界異常
6.ArrayList
底層是一個數組的結構,查詢快,但是增刪慢。多線程,查詢快
7.linkedList
Demo01LinkedList.java
package LinkedList;import java.util.LinkedList; import java.util.List;/* * java.util.LinkedList集合 implements List接口 * LinkedList集合的特點: * 1.底層是一個鏈表結構:查詢慢,增刪快 * 2.里面包含了大量操作首尾元素的方法 * 注意:使用LinkedList集合特有的方法,不能使用多態。 * -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) {/** public void addFirst(E e) :將指定元素插入此列表的開頭。=push* -public void addLast(E e):將指定元素添加到此列表的結尾。=add* -public void push(E e):將元素推入此列表所表示的堆棧。* *///創建的時候不能用多態LinkedList<String> linked = new LinkedList<>();linked.add("a");linked.add("b");linked.add("c");System.out.println(linked);//[a, b, c]linked.addFirst("d");System.out.println(linked);//[d, a, b, c]linked.addLast("e");System.out.println(linked);//[d, a, b, c, e]linked.push("f");System.out.println(linked);//[f, d, a, b, c, e]/** -public E getFirst():返回此列表的第一個元素。* -public E getLast():返回此列表的最后一個元素。* */System.out.println(linked.getFirst());//fSystem.out.println(linked.getLast());//e/** * -public E removeFirst():移除并返回此列表的第一個元素。* -public E removeLast():移除并返回此列表的最后一個元素。* -public E pop():從此列表所表示的堆棧處彈出一個元素。* */System.out.println(linked.removeFirst());//fSystem.out.println(linked.removeLast());//eSystem.out.println(linked);//[d, a, b, c]System.out.println(linked.pop());//dSystem.out.println(linked.isEmpty());//false} }8.Vector
同步,單線程,速度慢
總結
以上是生活随笔為你收集整理的2021-11-16数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021-11-14泛型
- 下一篇: 2021-11-18哈希值