SUMMARY | JAVA中的資料結構

來源:互聯網
上載者:User

標籤:get   override   雜湊   pop   def   構造   str   java   需要   

String

  • String類是不可修改的,建立需要修改的字串需要使用StringBuffer(線程同步,安全性更高)或者StringBuilder(線程非同步,速度更快)。
  • 可以用“+”串連String。
  • 用String.format()建立格式化字串。

ArrayList

  • 動態數組,可以動態增加或者減少元素。
  • 常用操作:
    • 建立:ArrayList<Type> arrayList=new ArrayList<>();
    • 添加元素:add
    • 擷取元素:get
    • 刪除元素:remove
    • 擷取數組大小:size
    • 判斷是否包含某一元素:contains
    • 設定元素:set
    • 清空數組:clear
    • 判斷是否為空白:isEmpty
  • 遍曆方式:通過下標索引效率最高
  • Type temp;for(int i=0;i<arrayList.size();i++){    temp=arrayList.get(i);}
  • arrayList排序需要實現Comparator介面以實現自訂排序
  • Comparator<String> comparator=new Comparator<String>() {    @Override    public int compare(String o1, String o2) {        if(o1.compareTo(o2)>0) return 1;        if(o1.compareTo(o2)<0) return -1;        return 0;    }};

Stack

  • 由vector拓展而來
  • 常用操作:push,peak,pop,search,isEmpty
  • public static void stackTest() {    Stack<String> stack=new Stack<String>();    //將元素壓入棧頂    System.out.println(stack.push("haha"));    System.out.println(stack.push("yes"));    //棧頂元素的search結果為1,然後從棧頂向棧底依次遞增    System.out.println(stack.search("haha"));    //查看棧頂元素    System.out.println(stack.peek());    //彈出棧頂元素並返回棧頂元素    System.out.println(stack.pop());    //判斷棧是否非空    System.out.println(stack.isEmpty());}

Queue

  • 是一個介面,LinkedList實現Queue介面
  • 常用操作:offer,peek,poll,isEmpty
  • public static void queueTest() {    //LinkedList實現了Queue介面,所以構造時使用LinKedList    Queue<String> queue=new LinkedList<String>();    //在隊列尾部增加元素,並返回是否增加成功    System.out.println(queue.offer("abc"));    System.out.println(queue.offer("def"));    //返回隊首元素,若隊列空,則返回null;(ps:element在隊列空時拋出異常)    System.out.println(queue.peek());    //彈出隊首元素,並返回隊首元素,若隊列空則返回null    System.out.println(queue.poll());    //判斷隊列是否空    System.out.println(queue.isEmpty());}

LinkedList

  • 雙向鏈表,非同步
  • 常用操作:addFirst,addLast,add,getFirst,getLast,get,removeFirst,removeLast,remove,isEmpty
    public static void linkedlistTest() {    LinkedList<String> linkedList=new LinkedList<String>();    //在鏈表頭部加,尾部加,任意位置加元素    linkedList.addFirst("haha");    linkedList.addLast("yali");    linkedList.add(1, "good");    //輸出鏈表    System.out.println("first element:"+linkedList.getFirst());    System.out.println("last element:"+linkedList.getLast());    for(int i=0;i<linkedList.size();i++) System.out.println(linkedList.get(i));    //刪除任意位置,頭部,尾部的鏈表元素    linkedList.remove(1);    linkedList.removeFirst();    linkedList.removeLast();    //判斷鏈表是否空    System.out.println(linkedList.isEmpty());}

HashMap

  • 非同步
  • public static void hashmapTest() {    HashMap<String, Integer> hashMap=new HashMap<String, Integer>();    //向雜湊表中添加元素    hashMap.put("tp", 123);    hashMap.put("zp", 567);    System.out.println(hashMap);    //通過迭代器便利雜湊表    for(Iterator<Entry<String, Integer>> iterator=hashMap.entrySet().iterator();iterator.hasNext();){        Entry<String, Integer> entry=iterator.next();        System.out.println(entry.getKey());        System.out.println(entry.getValue());    }    //尋找雜湊表中是否有該鍵    System.out.println(hashMap.containsKey("he"));    //尋找雜湊表是否有該值    System.out.println(hashMap.containsValue(567));    //刪除雜湊表中的索引值對    hashMap.remove("tp");    System.out.println(hashMap);}

     

SUMMARY | JAVA中的資料結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.