日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java 集合框架(List、Set、Map、Iterator、Stack、Properties)

發(fā)布時(shí)間:2024/7/5 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 集合框架(List、Set、Map、Iterator、Stack、Properties) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. ArrayList
    • 2. LinkedList
    • 3. HashSet
    • 4. TreeSet
    • 5. Iterator、ListIterator
    • 6. HashMap
    • 7. TreeMap
    • 8. Stack
    • 9. Properties 類(lèi)
      • 讀寫(xiě)簡(jiǎn)單 數(shù)據(jù)庫(kù)

相關(guān)文獻(xiàn):https://www.runoob.com/java/java-collections.html

1. ArrayList

  • 類(lèi)似動(dòng)態(tài)數(shù)組
ArrayList al = new ArrayList();System.out.println("al 的初始大小:" + al.size());// al 的初始大小:0al.add("C");al.add("A");al.add("E");al.add("B");al.add("D");al.add("F");al.add(1,"A2");System.out.println("al size: " + al.size());// al size: 7System.out.println("al 的內(nèi)容:" + al);// al 的內(nèi)容:[C, A2, A, E, B, D, F]al.remove("F");al.remove(2);System.out.println("al size: " + al.size());// al size: 5System.out.println("al 的內(nèi)容:" + al);// al 的內(nèi)容:[C, A2, E, B, D]ArrayList al1 = new ArrayList();al1.add(1);al1.add(2);al1.add(3);al1.add(4);System.out.println("al1 的內(nèi)容:" + al1);// al1 的內(nèi)容:[1, 2, 3, 4]Object o[] = al1.toArray();int sum = 0;for(int i = 0; i < o.length; ++i)sum += ((Integer)o[i]).intValue();System.out.println("sum: " + sum); // sum: 10

2. LinkedList

  • 鏈表
LinkedList ll = new LinkedList();ll.add("F");ll.add("B");ll.add("D");ll.add("E");ll.add("C");ll.addLast("Z");ll.addFirst("A");ll.add(1, 1);System.out.println(ll);// [A, 1, F, B, D, E, C, Z]ll.remove("F");ll.remove(2);System.out.println(ll);// [A, 1, D, E, C, Z]ll.removeFirst();ll.removeLast();System.out.println(ll);// [1, D, E, C]ll.set(2, "changed");System.out.println(ll);// [1, D, changed, C]

3. HashSet

  • 哈希集合,無(wú)序
HashSet hs = new HashSet();hs.add("A");hs.add("B");hs.add("C");hs.add(1);System.out.println(hs);// [A, 1, B, C] 無(wú)序

4. TreeSet

  • 樹(shù)set,有序
TreeSet ts = new TreeSet();ts.add("C");ts.add("B");ts.add("D");ts.add("A");ts.add("Aa");System.out.println(ts);// [A, Aa, B, C, D] 有序

5. Iterator、ListIterator

  • ListIterator 可以修改元素,可以雙向遍歷,是 Iterator 的擴(kuò)展
System.out.println(al);// [C, A2, E, B, D]Iterator it = al.iterator();while (it.hasNext()){Object obj = it.next();System.out.print(" "+ obj);} // 使用 iterator 遍歷// C A2 E B DSystem.out.println();ListIterator lit = al.listIterator();while(lit.hasNext()){Object obj = lit.next();lit.set(obj+"*");}System.out.println("打印修改后的內(nèi)容:");it = al.iterator();while (it.hasNext()){Object obj = it.next();System.out.print(" "+ obj);} // C* A2* E* B* D*System.out.println();System.out.println("逆序輸出:");while(lit.hasPrevious()){Object obj = lit.previous();System.out.print(" "+ obj);} // D* B* E* A2* C*System.out.println();

6. HashMap

// HashMapHashMap hm = new HashMap();hm.put("Michael", 18);hm.put("Ming", 19);Set set = hm.entrySet();Iterator i = set.iterator();while(i.hasNext()){Map.Entry me = (Map.Entry) i.next();System.out.print(me.getKey() + ":");System.out.println(me.getValue());}int age = ((Integer)hm.get("Michael")).intValue();hm.put("Michael", age+2);i = set.iterator();while(i.hasNext()){Map.Entry me = (Map.Entry) i.next();System.out.print(me.getKey() + ":");System.out.println(me.getValue());}

輸出:

Ming:19 Michael:18 Ming:19 Michael:20

7. TreeMap

// TreeMapTreeMap tm = new TreeMap();tm.put(18, "Michael");tm.put(19, "Ming");tm.put(0, "Java");// valuesCollection col = tm.values();Iterator it1 = col.iterator();while(it1.hasNext()){System.out.println(it1.next());}// keySetCollection col1 = tm.keySet();Iterator it2 = col1.iterator();while(it2.hasNext()){System.out.println(it2.next());}// entrySet, K V 對(duì)Collection col2 = tm.entrySet();Iterator it3 = col2.iterator();while(it3.hasNext()){System.out.println(it3.next());}

輸出:

Java Michael Ming 0 18 19 0=Java 18=Michael 19=Ming

8. Stack

  • Stack 繼承于 Vector,Vector 與 ArrayList 類(lèi)似
class StackDemo{static void showpush(Stack st, int a){st.push(a);System.out.println("入棧:" + a);System.out.println(st);}static void showpop(Stack st){Integer a = (Integer) st.pop();System.out.println("出棧:" + a);System.out.println(st);}public static void main(String [] args){Stack st = new Stack();System.out.println(st);showpush(st,2);showpush(st,4);showpush(st,1);showpop(st);showpop(st);showpop(st);try{showpop(st);//空了,異常}catch (EmptyStackException e){System.out.println("異常:" + e);}} }

輸出:

[] 入棧:2 [2] 入棧:4 [2, 4] 入棧:1 [2, 4, 1] 出棧:1 [2, 4] 出棧:4 [2] 出棧:2 [] 異常:java.util.EmptyStackException

9. Properties 類(lèi)

// Properties : k v 都是字符串的 HashtableProperties capitals = new Properties();capitals.put("中國(guó)", "北京");capitals.put("日本", "東京");// capitals.put("美國(guó)", "華盛頓");Set states = capitals.keySet();String country;Iterator it4 = states.iterator();while(it4.hasNext()){country = (String) it4.next();System.out.println(country + " : " + capitals.getProperty(country));}String str = capitals.getProperty("美國(guó)", "not found");//若沒(méi)有key,返回默認(rèn)值 not foundSystem.out.println(str);

輸出:

中國(guó) : 北京 日本 : 東京 not found

讀寫(xiě)簡(jiǎn)單 數(shù)據(jù)庫(kù)

  • 特別適合做簡(jiǎn)單數(shù)據(jù)庫(kù)
class PropertiesFile{public static void main(String [] args){Properties settings = new Properties();try{settings.load(new FileInputStream("./count.txt"));}catch (Exception e){settings.setProperty("count", new Integer(0).toString());//沒(méi)有文件就從0開(kāi)始計(jì)數(shù)}int c = Integer.parseInt(settings.getProperty("count"))+1;System.out.println("這是第" + c + "次使用本程序!");settings.put("count", new Integer(c).toString());try{settings.store(new FileOutputStream("./count.txt"), "存儲(chǔ)中");}catch (Exception e){System.out.println(e.getMessage());}} }

輸出:

這是第1次使用本程序!

總結(jié)

以上是生活随笔為你收集整理的Java 集合框架(List、Set、Map、Iterator、Stack、Properties)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。