閱讀704 返回首頁    go 阿裏雲 go 技術社區[雲棲]


【Java學習係列】第3課--Java 高級教程

java數據結構

 

  1)【概述】

  Java的工具包提供了強大的數據結構在的Java中的數據結構主要包括以下幾種接口和類:

  • 枚舉(枚舉)

  • 位集合(位集合)

  • 向量(矢量)

  • 棧(棧)

  • 字典(詞典)

  • 哈希表(哈希表)

  • 屬性(屬性)

  以上這些類是傳統遺留的,在Java2的中引入了一種新的框架 - 集合框架(集合),我們後麵再討論。

 

  2)枚舉(枚舉)

    a)【定義】枚舉(枚舉)接口雖然它本身不屬於數據結構,但它在其他數據結構的範疇裏應用很廣。枚舉(枚舉)接口定義了一種從數據結構中取回連續元素的方式

    B)【代碼舉例】

import java.util.Vector; import java.util.Enumeration; public class EnumerationTester {public static void main(String args []){ 
      枚舉天數; 
      Vector dayNames = new Vector(); 
      dayNames.add( “星期天”); 
      dayNames.add( “星期一”); 
      dayNames.add( “星期二”); 
      dayNames.add( “星期三”); 
      dayNames.add( “星期四”); 
      dayNames.add( “星期五”); 
      dayNames.add( “星期六”); 
      days = dayNames.elements(); while(days.hasMoreElements()){ 
         System.out.println(days.nextElement()); 
      } 
   } 
}

EnumerationTester.java

     運行結果:

周日
星期一
星期二
星期三
星期四
星期五
星期六

運行結果

如果你想學習Java可以來這個群,首先是二二零,中間是一四二,最後是九零六,裏麵有大量的學習資料可以下載。

 

  3)位集合(位集合)

    A)【定義】

      位集合類實現了一組可以單獨設置和清除的位或標誌。該類在處理一組布爾值的時候非常有用,你隻需要給每個值賦值一 “位”,然後對位進行適當的設置或清除,就可以對布爾值進行操作了。

    B)【代碼舉例】

import java.util.BitSet; public class BitSetDemo {public static void main(String args []){ 
     BitSet bits1 = new BitSet(16); 
     BitSet bits2 = new BitSet(16);      
     //設置一些位
     (int i = 0; i <16; i ++){if((i%2)== 0)bits1.set(i); if((i%5)!= 0)bits2.set(i); 
     } 
     System.out.println(“bit1:”中的初始模式); 
     的System.out.println(BITS1); 
     System.out.println(“\ nInitial pattern in bits2:”); 
     的System.out.println(BITS2); // AND bits 
     bits2.and(bits1); 
     System.out.println(“\ nbits2 AND bits1:”); 
     的System.out.println(BITS2); // OR bits 
     bits2.or(bits1); 
     System.out.println(“\ nbits2 OR bits1:”); 
     的System.out.println(BITS2); // XOR bits 
     bits2.xor(bits1); 
     System.out.println(“\ nbits2 XOR bits1:”); 
     的System.out.println(BITS2); 
  } 
}

BitSetDemo.java

  運行結果:

位1中的初始模式:
{ 0,2,4,6,8,10,12,14 }位2中的
初始模式:
{ 1,2,3,4,6,7,8,9,11,12,13 , 14} 

2 AND位1:{ 2,4,6,8,12,14 } 位2 
OR位1:

0,2,4,6,8,10,12,14 } 位2 XOR位1:
{}

運行結果

 

 4)向量(Vector)

  A)【定義】

    向量(向量)類和傳統數組非常相似,但是矢量的大小能根據需要動態的變化。

    和數組一樣,矢量對象的元素也能通過索引訪問。

    使用矢量類最主要的好處就是在創建對象的時候不必給對象指定大小,它的大小會根據需要動態的變化。

 

  B)【代碼實例】

import java.util。*; public class VectorDemo {public static void main(String args []){//初始大小為3,增量為2 
      Vector v = new Vector(3,2); 
      System.out.println(“初始大小:”+ v.size()); 
      System.out.println(“初始容量:”+ 
      v.capacity()); 
      v.addElement(new Integer(1)); 
      v.addElement(new Integer(2)); 
      v.addElement(new Integer(3)); 
      v.addElement(new Integer(4)); 
      System.out.println(“四個添加後的容量:”+ 
          v.capacity()); 
      v.addElement(new Double(5.45)); 
      System.out.println(“Current capacity:”+ 
      v.capacity()); 
      v.addElement(new Double(6.08)); 
      v.addElement(new Integer(7)); 
      System.out.println(“Current capacity:”+ 
      v.capacity()); 
      v.addElement(new Float(9.4)); 
      v.addElement(new Integer(10)); 
      System.out.println(“Current capacity:”+ 
      v.capacity()); 
      v.addElement(new Integer(11)); 
      v.addElement(new Integer(12)); 
      System.out.println(“First element:”+ 
         (Integer)v.firstElement()); 
      System.out.println(“Last element:”+ 
         (Integer)v.lastElement()); if(v.contains(new Integer(3)))
         System.out.println(“Vector contains 3”); //枚舉向量中的元素。
      枚舉vEnum = v.elements(); 
      System.out.println(“\ nElements in vector:”); while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement()+“”); 
      的System.out.println(); 
   } 
}

VectorDemo.java

    運行結果:

初始容量:0 
初始容量:3 
四次加載後的容量:5 
電流容量:5 
電流容量:7 
電流容量:9 
第一元素:1 
最後元素:12 
矢量包含3.矢量中的
元素:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

運行結果

 

 

5)棧(堆棧)

  A)【定義】

    棧(堆棧)實現了一個後進先出(LIFO)的數據結構。

    你可以把棧理解為對象的垂直分布的棧,當你添加一個新元素時,就將新元素放在其他元素的頂部。

    當你從棧中取元素的時候,就從棧頂取一個元素。換句話說,最後進棧的元素最先被取出。

 

  B)【代碼實例】

import java.util。*; public class StackDemo {static void showpush(Stack st,int a){ 
      st.push(new Integer(a)); 
      System.out.println(“push(”+ a +“)”); 
      System.out.println(“stack:”+ st); 
   } static void showpop(Stack st){ 
      System.out.print(“pop  - >”); 
      整數a =(整數)st.pop(); 
      的System.out.println(一); 
      System.out.println(“stack:”+ st); 
   } public static void main(String args []){ 
      Stack st = new Stack(); 
      System.out.println(“stack:”+ st); 
      showpush(st,42); 
      showpush(st,66); 
      showpush(st,99); 
      showpop(ST); 
      showpop(ST); 
      showpop(ST); 嚐試{ 
         showpop(st); 
      } catch(EmptyStackException e){ 
         System.out.println(“empty stack”); 
      } 
   } 
}

StackDemo.java

    運行結果:

堆棧:[] 
推送(42)
堆棧:[42] 
推(66)
堆棧:[42,66] 
推(99)
堆棧:[42,66,99] 
彈出- > 99 
堆棧:[42,66] 
彈出- > 66 
堆棧:[42] 
pop  - > 42 
堆棧:[] 
pop  - >空堆棧

運行結果

 

6)字典(詞典)

  A)【定義】

    字典(字典)類是一個抽象類,它定義了鍵映射到值的數據結構。

    當你想要通過特定的鍵而不是整數索引來訪問數據的時候,這時候應該使用字典。

    由於字典類是抽象類,所以它隻提供了鍵映射到值的數據結構,而沒有提供特定的實現。

 

7)哈希表(哈希表)

  A)【定義】  

    Hashtable的類提供了一種在用戶定義鍵結構的基礎上來組織數據的手段。

    例如,在地址列表的哈希表中,你可以根據郵政編碼作為鍵來存儲和排序數據,而不是通過人名。

 

  B)【代碼實現】

import java.util。*; public class HashTableDemo {public static void main(String args []){//創建一個哈希映射
      Hashtable balance = new Hashtable(); 
      枚舉名稱 
      字符串str 雙平; 
      balance.put(“Zara”,new Double(3434.34)); 
      balance.put(“Mahnaz”,new Double(123.22)); 
      balance.put(“Ayan”,新雙人(1378.00)); 
      balance.put(“Daisy”,new Double(99.22)); 
      balance.put(“Qadir”,new Double(-19.08)); //顯示哈希表中的所有餘額。
      names = balance.keys(); while(names.hasMoreElements()){ 
         str =(String)names.nextElement(); 
         System.out.println(str +“:”+ 
         balance.get(str)); 
      } 
      System.out.println(); //將1,000存入Zara的帳戶
      bal =((Double)balance.get(“Zara”))。doubleValue(); 
      balance.put(“Zara”,new Double(bal + 1000)); 
      System.out.println(“Zara的新餘額:”+ 
      balance.get(“Zara”)); 
   } 
}

HashTableDemo.java

  運行結果如下:

Qadir:-19.08 
Zara:3434.34 
Mahnaz:123.22 
Daisy:99.22 
Ayan:1378.0 
Zara的新餘額:4434.34

運行結果

 

8)哈希表(哈希表)

  A)【定義】

 

    屬性繼承於Hashtable.Properties類表示了一個持久的屬性集。屬性列表中每個鍵及其對應值都是一個字符串。

 

    屬性類被許多Java類使用。例如,在獲取環境變量時它們就作為System.getProperties()方法的返回值。

  B)【代碼實現】

import java.util。*; public class PropDemo {public static void main(String args []){ 
      Properties capitals = new Properties(); 
      設置狀態 
      字符串str 
      
      capitals.put(“Illinois”,“Springfield”); 
      capitals.put(“密蘇裏州”,“傑斐遜城”); 
      首都(“華盛頓”,“奧林匹亞”); 
      capitals.put(“California”,“Sacramento”); 
      capitals.put(“Indiana”,“Indianapolis”); //在哈希表中顯示所有州和首都。
      states = capitals.keySet(); //獲取鍵的set-view 
      Iterator itr = states.iterator(); while(itr.hasNext()){ 
         str =(String)itr.next(); 
         System.out.println(“”+ 
            str + “的大寫是”+ capitals.getProperty(str)+“。”); 
      } 
      System.out.println(); //尋找狀態不在列表中 - 指定默認的
      str = capitals.getProperty(“Florida”,“Not Found”); 
      System.out.println(“佛羅裏達的首都是” 
          + str +“。”); 
   } 
}

PropDemo.java

 

  運行結果如下:

密蘇裏州首府是傑斐遜城。
伊利諾伊州首府是斯普林菲爾德。
印第安納州首府是印第安納波利斯。
加利福尼亞的首都是薩克拉門托。
華盛頓的首府是奧林匹亞。
佛羅裏達州的首府未找到。

最後更新:2017-06-20 09:02:07

  上一篇:go  給Java新手的一些建議——Java知識點歸納(Java基礎部分)
  下一篇:go  Java中this和super的用法總結