Java 備忘知識

來源:互聯網
上載者:User

標籤:

待補充 ........

0:常用標頭檔(待補充)

import java.util.Arrays;import java.util.HashSet;import java.util.TreeSet;import java.util.Map;import java.util.HashMap;import java.io.InputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.IOException;import java.util.StringTokenizer;import java.math.BigInteger;               //大整數import java.math.BigDecimal;            //大浮點數

 

1:讀入(from Petr)

class InputReader {    public BufferedReader reader;    public StringTokenizer tokenizer;    public InputReader(InputStream stream) {        reader = new BufferedReader(new InputStreamReader(stream), 32768);        tokenizer = null;    }    public String next() {        while (tokenizer == null || !tokenizer.hasMoreTokens()) {            try {                tokenizer = new StringTokenizer(reader.readLine());            } catch (IOException e) {                throw new RuntimeException(e);            }        }        return tokenizer.nextToken();    }    public int nextInt() {        return Integer.parseInt(next());    }}
View Code

 

2:高精度的使用

 

3:Set 類的使用

  介紹:Java中的Set有三種常用的實現方式:

  (1)HashSet : HashSet將資料存放區雜湊表中,效能最佳。(無序)

  (2)TreeSet:   TreeSet將資料存放區在紅/黑樹狀結構中,資料的順序依據資料的值。(有序)

  (3)LinkedHashSet:   LinkedHashSet中的資料存放區在雜湊表中,同時被鏈表貫穿著。它的順序是按照插入的順序。(有序)

  使用:

  (1)初始化、插入、刪除、遍曆

Set<Type> set = new HashSet<Type>();    //HashSet 初始化Set<Type> set = new TreeSet<Type>();    //TreeSet 初始化set.add(Type);       //元素插入set.remove(Type);    //元素刪除set.contains(Type);  //如果包含某個元素,則返回 trueset.size();          //返回集合中的元素個數set.clear();         //清空集合set.isEmpty();       //如果集合為空白,則返回 true//遍曆方法一:迭代器遍曆for(Iterator<Type> iterator = set.iterator();iterator.hasNext();){     System.out.print(iterator.next() + " " );  }//遍曆方法二:迴圈遍曆for(Type value : set){       System.out.print(value+" ");  }

  (2)TreeSet 自訂比較方法

Set<Type> set = new TreeSet<Type>(new MyComparator());  //新的初始化方法class MyComparator implements Comparator<Type>{    //自訂類    public int compare(Type a, Type b) {          return ;  //自訂比較方法    }  }  

 

4:Map 類的使用(推薦部落格、推薦部落格2)

  介紹:HashMap通過hashcode對其內容進行快速尋找,而 TreeMap中所有的元素都保持著某種固定的順序。

    如果你需要得到一個有序的結果你就應該使用TreeMap(HashMap中元素的排列順序是不固定的)。

  (1)初始化、插入、刪除、遍曆

Map<Type1, Type2> map = new HashMap<Type1, Type2>();  //初始化map.put(Type1 , Type2);     //插入map.remove(Type1);          //刪除map.clear();                //清空map.get(Type1);             //返回與Type1相關的Type2map.containsKey(Type1);     //如果包含指定鍵的映射,則返回 truemap.containsValue(Type2);   //如果將一個或多個鍵映射到指定值,則返 true,效率低map.isEmpty();              //如果 Map 為空白,則範圍 truemap.size();                  //返回 Map 的映射數目map.putAll(Map t);          //將 t 中的所有映射複製過來//遍曆主方法一:在for-each迴圈中使用entries來遍曆for (Map.Entry<Integer, Integer> entry : map.entrySet()) {    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}//遍曆主方法二:在for-each迴圈中遍曆keys或values(效率稍高) for (Integer key : map.keySet()) {  //僅需要鍵    System.out.println("Key = " + key);} //遍曆map中的值 for (Integer value : map.values()) {  //僅需要值    System.out.println("Value = " + value);}

 

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.