<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                軟件中的數據可以說是非常重要的一部分,而我們組織和操作數據的一般都是java.util包中的一些集合類,這些集合我們稱為容器。? ### 一:數組與集合 數組其實也是我們經常用來組織和操作數據的類,但是數組與集合類的不同有二個不同的地方: 1. ?數組的長度是固定不變的,集合的長度是可變的。 1. ?數組用來存放基本的類型數據,集合用來存放對象的引用。 ### 二:集合 ? ?? Java容器類包含Collection,??List(鏈表), ArrayList, LinkedList ,Vector(向量),Stack(堆棧),?Set(集合), ?TreeSet, HashSet,Map(映射表), TreeMap, HashMap, Queue(隊列),?Deque(雙隊列)。 Collection |----List | ? ? ?|----ArrayList | ? ? ?|----LinkedList | ? ? ?|----Vector | ? ? ? ? ? ? ?|----Stack |----Set | ? ? |----HashSet | ? ? |----SortedSet | ? ? ? ? ? ?|----TreeSet |----Queue | ? ? |----Deque Map |----HashMap |----TreeMap #### 2.1 Collection接口 Interface Collection<E>: Collection接口是一個根接口,它提供了對數據的add,remove,iterator()方法。 ~~~ Collection list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); Iterator it = list.iterator(); while(it.hasNext()){ String str = (String) it.next(); System.out.println(str); } ~~~ #### 2.2 ?List(鏈表), ArrayList,LinkedList,Vector,Stack List接口相當于鏈表,是有序的Collection,其中的元素可重復,各元素的順序是對象插入的順序。用戶能夠使用索引(元素在List中的位置,類似于數組下標)來訪問List中的元素,這類似于Java的數組。? 實現List接口的常用類有LinkedList,ArrayList,Vector和Stack。 Collection |----List ? ? ? ? ? ?|----LinkedList? ? ? ? ? ? ?|----ArrayList ? ? ? ? ? ?|----Vector ? ? ? ? ? ? ? ? ? ?|----Stack? ArrayList類: ArrayList實現了可變的數組,允許所有元素,包括null.并可以根據索引位置對集合進行快速的隨機訪問。缺點是向指定的索引位置插入對象或刪除對象的速度較慢。所以,它適合隨機查找和遍歷,不適合插入和刪除。ArrayList是非同步的。 LinkList類: LinkList類采用了鏈表的結構保存對象。所以便于向集合中插入和刪除對象,需要向集合中插入和刪除對象時,LinkList類效率較好,但是對于隨機訪問集合中的對象,LinkList效率慢。所以,它適合數據的動態插入和刪除,不適合隨機訪問和遍歷,LinkList也是非同步的。 Vector類: Vector非常類似ArrayList,但是Vector是支持線程同步的。即某一時刻只有一個線程能夠寫Vector,避免多線程同時寫而引起的不一致性,但實現同步需要很高的花費,因此,訪問它比訪問ArrayList慢。所以,它適合對線程同步有要求的數據,但是效率比ArrayList要慢。 ~~~ Vector vector = new Vector(); vector.addElement("one"); vector.addElement("two"); vector.addElement("three"); vector.insertElementAt("zero",0); vector.insertElementAt("oop",3); vector.setElementAt("three",3); vector.setElementAt("four",4); //vector.removeAllElements(); printData(vector.iterator()); ~~~ ~~~ private static void printData(Iterator it){ while(it.hasNext()){ String str = (String) it.next(); System.out.println(str); } } ~~~ Stack類: Stack是Vector的一個子類,它實現標準的后進先出堆棧。 ~~~ Stack stack = new Stack(); stack.push(new Integer(11111)); printStack(stack); stack.push("absdder"); printStack(stack); stack.push(new Double(29999.3)); printStack(stack); stack.push(220.2); printStack(stack); String s = new String("absdder"); System.out.println("元素absdder在堆棧的位置"+stack.search(s)); System.out.println("元素11111在堆棧的位置"+stack.search(11111)); System.out.println("元素"+stack.pop()+"出棧"); printStack(stack); System.out.println("元素"+stack.pop()+"出棧"); printStack(stack); System.out.println("元素"+stack.pop()+"出棧"); printStack(stack); ~~~ ~~~ private static void printStack(Stack stack) { // TODO Auto-generated method stub if (stack.empty()) System.out.println("堆棧是空的,沒有元素"); else { System.out.print("堆棧中的元素:"); Enumeration items = stack.elements(); while (items.hasMoreElements()) System.out.print(items.nextElement()+" "); } System.out.println(); } ~~~ ### 2.3 Set(集合),SortedSet(有序集合),HashSet,TreeSet, Collection |----Set ? ? ? ?|----HashSet ? ? ? ?|----SortedSet ? ? ? ? ? ? ? ?|----TreeSet Set集合: Set接口,Set集合中的對象不按特定的方式排序,只是簡單的把對象加入到集合中,但是Set集合中不能包含重復對象。 SortedSet(有序集合): SortedSet接口擴展了Set并說明了按升序排列的集合的特性? HashSet: HashSet,由哈希表(實際是一個HashMap實例)。它不保證Set的迭代順序,特別是不保證此順序不變。 TreeSet: TreeSet類實現了Set和SortedSet接口,因此,能對集合的元素進行排序。 ~~~ Set set = new TreeSet(); set.add("first_item"); set.add("second_item"); set.add("third_item"); Iterator it = set.iterator(); while(it.hasNext()){ String str = (String) it.next(); System.out.println(str); } ~~~ ### 2.4 Map,HashMap,TreeMap |----Map | ? ? |----HashMap | ? ? |----TreeMap Map: Map沒有繼承Collection接口,Map提供key到value的映射。一個Map中不能包含相同的key,每個key只能映射一個 value。Map接口提供3種集合的視圖,Map的內容可以被當作一組key集合,一組value集合,或者一組key-value映射。 HashMap: HashMap類實現了Map集合,是基于哈希表的Map接口實現,允許使用null值和null鍵。它實現的Map集合對于添加和刪除映射關系效率比較高,所以用的比較多。但是HashMap不能保證映射的順序。 TreeMap: TreeMap類實現了Map和SortedMap接口,因此映射的關系有一定的順序,但是添加,刪除和定位映射關系時,性能比HashMap差。由于TreeMap映射有一定的順序,所以不允許鍵對象是null。 ~~~ HashMap hashmap = new HashMap(); hashmap.put("key_Item0","Value0"); hashmap.put("key_Item1","Value1"); hashmap.put("key_Item2","Value2"); hashmap.put("key_Item3","Value3"); Set set = hashmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); System.out.println(mapentry.getKey()+"/"+mapentry.getValue()); } set = hashmap.keySet(); Iterator it = set.iterator(); while(it.hasNext()){ String str = (String) it.next(); System.out.println(str); } Collection collection = hashmap.values(); it = collection.iterator(); while(it.hasNext()){ String str = (String) it.next(); System.out.println(str); } ~~~ ### 2.5 queue(隊列), Deque(雙隊列) |----Collection | ? ? |----Queue | ? ? |----Deque queue(隊列): 隊列是一種數據結構.它有兩個基本操作:在隊列尾部加人一個元素,和從隊列頭部移除一個元素就是說,隊列以一種先進先出的方式管理數據。 ~~~ Queue<String> queue = new LinkedList<String>(); //添加元素 queue.offer("a"); queue.offer("b"); queue.offer("c"); queue.offer("d"); queue.offer("e"); for(String q : queue){ System.out.println(q); } System.out.println("==="); System.out.println("poll="+queue.poll()); //返回第一個元素,并在隊列中刪除 for(String q : queue){ System.out.println(q); } System.out.println("==="); System.out.println("element="+queue.element()); //返回第一個元素 for(String q : queue){ System.out.println(q); } System.out.println("==="); System.out.println("peek="+queue.peek()); //返回第一個元素 for(String q : queue){ System.out.println(q); } ~~~ Deque(雙隊列): Deque(雙隊列),或雙端隊列,是需要在隊列的二端都能插入和刪除數據,但是不需要在中隊列的中間插入和刪除數據的場合所使用的一種數據結構。 ### 三.參考資料: (1)Java 從入門到精通 第二版 清華大學出版社 李鐘尉,周小彤編著 第14章 集合類 (2)java api [http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4](http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4) (3)Java容器——JSTL學習筆記 [http://www.cnblogs.com/viviman/archive/2013/01/14/2860198.html](http://www.cnblogs.com/viviman/archive/2013/01/14/2860198.html) (4)Java容器集合類的區別用法 [http://www.cnblogs.com/sunliming/archive/2011/04/05/2005957.html](http://www.cnblogs.com/sunliming/archive/2011/04/05/2005957.html) (5)游戲編程精粹1 1.4 在游戲編程中使用STL
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看