Java高級部分總結(jié)

1、數(shù)據(jù)結(jié)構(gòu)

Java工具包提供了強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)。在Java中的數(shù)據(jù)結(jié)構(gòu)主要包括以下幾種接口和類:

枚舉、位集合、向量、棧、字典、哈希表、屬性。

?

枚舉 Enumeration?

Enumeration接口定義了一些方法,通過這些方法可以枚舉(一次獲得一個(gè))對象集合中的元素。下面總結(jié)了一些Enumeration聲明的方法:

1、boolean hasMoreElements( )

?測試此枚舉是否包含更多的元素。

2、Object nextElement( )

如果此枚舉對象至少還有一個(gè)可提供的元素,則返回此枚舉的下一個(gè)元素。

例:

package Test_hello;

import java.util.Vector;

import java.util.Enumeration;??//導(dǎo)入Enumeration接口

public class Hello {

???????? ??? public static void main(String[] args) {??? //主函數(shù)

?Enumeration days;

?Vector dayNames = new Vector();

???????? ??????? dayNames.add("Sunday");

???????? ??????? dayNames.add("Monday");

???????? ??????? dayNames.add("Tuesday");

???????? ??????? dayNames.add("Wednesday");

???????? ??????? dayNames.add("Thursday");

???????? ??????? dayNames.add("Friday");

???????? ??????? dayNames.add("Saturday");

???????? ??????? days = dayNames.elements();

???????? ??????? while (days.hasMoreElements()){

System.out.println(days.nextElement());

???????? ??????? }

???????? ? ?}

}

結(jié)果:

Sunday、Monday、Tuesday、Wednesday、Thursday、Friday、Saturday

?

向量Vector

Vector 類實(shí)現(xiàn)了一個(gè)動態(tài)數(shù)組,主要用在事先不知道數(shù)組的大小,或者只是需要一個(gè)可以改變大小的數(shù)組的情況。

例:

package Test_hello;

import java.util.Vector;

import java.util.Enumeration;?? //導(dǎo)入Enumeration接口


public class Hello {

???????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? ?Vector v = new Vector(3, 2);

???????? ???????? System.out.println("Initial size: " + v.size());???? //0

???????? ???????? System.out.println("Initial capacity: " + v.capacity());? //3

???????? ??? ?????v.addElement(new Integer(1));

???????? ???????? v.addElement(new Integer(2));

???????? ???????? v.addElement(new Integer(3));

???????? ???????? v.addElement(new Integer(4));

???????? ???????? System.out.println("Capacity after four additions: " + v.capacity());? //5


???????? ???????? v.addElement(new Double(5.45));

???????? ???????? System.out.println("Current capacity: " + v.capacity()); //5

???????? ???????? v.addElement(new Double(6.08));

???????? ???????? v.addElement(new Integer(7));

???????? ???????? System.out.println("Current capacity: " + v.capacity()); //7

???????? ???????? v.addElement(new Float(9.4));

???????? ???????? v.addElement(new Integer(10));

???????? ???????? System.out.println("Current capacity: " + v.capacity());? //9

???????? ???????? v.addElement(new Integer(11));

???????? ???????? v.addElement(new Integer(12));

???????? ???????? System.out.println("First element: " + (Integer)v.firstElement());? //1

???????? ???????? System.out.println("Last element: " + ?(Integer)v.lastElement());? //12


if(v.contains(new Integer(3)))

???????? ???????? System.out.println("Vector contains 3.");

???????? ?????? ? ?Enumeration vEnum = v.elements();

???????? ???????? System.out.println("\nElements in vector:");

???????? ???????? while(vEnum.hasMoreElements())

???????? ??????????? System.out.print(vEnum.nextElement() + " ");

???????? ???????? System.out.println();

???????? ????? }

}

結(jié)果:

Initial size: 0

Initial capacity: 3

Capacity after four additions: 5

Current capacity: 5

Current capacity: 7

Current capacity: 9

First element: 1

Last element: 12

Vector contains 3.


Elements in vector:

1 2 3 4 5.45 6.08 7 9.4 10 11 12


棧 Stack?

棧是Vector的一個(gè)子類,它實(shí)現(xiàn)了一個(gè)標(biāo)準(zhǔn)的后進(jìn)先出的棧。

boolean empty()

測試堆棧是否為空。

Object peek( )

查看堆棧頂部的對象,但不從堆棧中移除它。

Object pop( )

移除堆棧頂部的對象,并作為此函數(shù)的值返回該對象。

Object push(Object element)

把項(xiàng)壓入堆棧頂部。

int search(Object element)

返回對象在堆棧中的位置,以 1 為基數(shù)。

例:

package Test_hello;

import java.util.*;


public class Hello {

???????? static void showpush(Stack<Integer> st, int a) {

??????? st.push(new Integer(a));

??????? System.out.println("push(" + a + ")");

??????? System.out.println("stack: " + st);

??? }


??? static void showpop(Stack<Integer> st) {

??????? System.out.print("pop -> ");

??????? Integer a = (Integer) st.pop();

??????? System.out.println(a);

??????? System.out.println("stack: " + st);

??? }


???????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? Stack<Integer> st = new Stack<Integer>();

???????? ??????? System.out.println("stack: " + st);?? // stack: [ ]

???????? ??????? showpush(st, 42);????? //push(42)? stack: [42]

???????? ??????? showpush(st, 66);???? //push(66)? stack: [42, 66]

???????? ??????? showpush(st, 99);???? //push(99)? stack: [42, 66, 99]

???????? ??????? showpop(st);?? //pop -> 99 ?stack: [42, 66]

???????? ??????? showpop(st);?? //pop -> 66? stack: [42]

???????? ??????? showpop(st);?? //pop -> 42?? stack: [ ]

???????? ??????? try {

???????? ??????????? showpop(st);

???????? ??????? } catch (EmptyStackException e) {??

???????? ??????????? System.out.println("empty stack");? //pop -> empty stack

???????? ??????? }

???????? ?}

}


字典 Map

Map類是一個(gè)抽象類,用來存儲鍵/值對。

給出鍵和值,你就可以將值存儲在Map對象中。一旦該值被存儲,就可以通過它的鍵來獲取它,可以作為一個(gè)鍵/值對列表。

例:

package Test_hello;

import java.util.*;


public class Hello {

??????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? Map m1 = new HashMap();

???????? ??????? m1.put("Zara", "8");

???????? ??????? m1.put("Mahnaz", "31");

???????? ??????? m1.put("Ayan", "12");

???????? ??????? m1.put("Daisy", "14");

???????? ??????? System.out.println();

???????? ??????? System.out.println(" Map Elements");

???????? ??????? System.out.print("\t" + m1);

???????? ?}

}

結(jié)果:

?Map Elements

???????? {Daisy=14, Ayan=12, Zara=8, Mahnaz=31}

?

哈希表 Hashtable?

Hashtable類提供了一種在用戶定義鍵結(jié)構(gòu)的基礎(chǔ)上來組織數(shù)據(jù)的手段。

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

哈希表鍵的具體含義完全取決于哈希表的使用情景和它包含的數(shù)據(jù)。

例:

package Test_hello;

import java.util.*;


public class Hello {

??????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ?? ? ????Hashtable balance = new Hashtable(); //實(shí)例化哈希表

???????? ??????? Enumeration names;

???????? ??????? String str;

???????? ??????? double bal;


???????? ??????? balance.put("Zara", new Double(3434.34));

???????? ??????? balance.put("Mahnaz", new Double(123.22));

???????? ??????? balance.put("Ayan", new Double(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));

???????? ????????? }


???????? ??????? bal = ((Double)balance.get("Zara")).doubleValue();? //返回指定鍵所映射到的值

???????? ??????? balance.put("Zara", new Double(bal+1000));??? //將指定 key 映射到此哈希表中的指定 value

???????? ??????? System.out.println("Zara's new balance: " + balance.get("Zara"));

???????? ?? }

}

結(jié)果:

Qadir: -19.08

Zara: 3434.34

Mahnaz: 123.22

Daisy: 99.22

Ayan: 1378.0


Zara's new balance: 4434.34



屬性 Properties?

Properties 繼承于 Hashtable.表示一個(gè)持久的屬性集.屬性列表中每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串。

Properties 類被許多Java類使用。例如,在獲取環(huán)境變量時(shí)它就作為System.getProperties()方法的返回值。

Properties 定義如下實(shí)例變量.這個(gè)變量持有一個(gè)Properties對象相關(guān)的默認(rèn)屬性列表。

例:

package Test_hello;

import java.util.*;


public class Hello {

??????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? Properties capitals = new Properties();? //實(shí)例化屬性類

???????? ??????? Set states;

???????? ??????? String str;

???????? ??????? capitals.put("Illinois", "Springfield");

???????? ??????? capitals.put("Missouri", "Jefferson City");

???????? ??????? capitals.put("Washington", "Olympia");

???????? ??????? capitals.put("California", "Sacramento");

???????? ??????? capitals.put("Indiana", "Indianapolis");


???????? ??????? states = capitals.keySet();???? ?// get set-view of keys

???????? ??????? Iterator itr = states.iterator();

???????? ??????? while(itr.hasNext()) {

???????? ?????????? str = (String) itr.next();

???????? ?????????? System.out.println("The capital of " +

???????? ????????????? str + " is " + capitals.getProperty(str) + ".");? //打印每個(gè)結(jié)果

???????? ??????? }


?????????? ?System.out.println(); ?//換行

???????? ??????? str = capitals.getProperty("Florida", "Not Found");?? //用指定的鍵在屬性列表中搜索屬性。

???????? ??????? System.out.println("The capital of Florida is " + str + ".");?


???????? ?}

}

結(jié)果:

The capital of Indiana is Indianapolis.

The capital of Illinois is Springfield.

The capital of Missouri is Jefferson City.

The capital of California is Sacramento.

The capital of Washington is Olympia.


The capital of Florida is Not Found.


位Bitset

位集合類實(shí)現(xiàn)了一組可以單獨(dú)設(shè)置和清除的位或標(biāo)志。

該類在處理一組布爾值的時(shí)候非常有用,你只需要給每個(gè)值賦值一"位",然后對位進(jìn)行適當(dāng)?shù)脑O(shè)置或清除,就可以對布爾值進(jìn)行操作了。

例:

package Test_hello;

import java.util.*;


public class Hello {

??????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? BitSet bits1 = new BitSet(16);

???????? ??????? BitSet bits2 = new BitSet(16);


???????? ??????? // set some bits

???????? ??????? for(int i=0; i<16; i++) {

???????? ?????????? if((i%2) == 0) bits1.set(i);

???????? ?????????? if((i%5) != 0) bits2.set(i);

???????? ??????? }

???????? ??????? System.out.println("Initial pattern in bits1: ");

???????? ??????? System.out.println(bits1);???? //打印bits1

???????? ??????? System.out.println("\nInitial pattern in bits2: ");

???????? ??????? System.out.println(bits2);???? //打印bits2


???????? ??????? // AND bits

???????? ??????? bits2.and(bits1);????? // bits1和 bits2 相與,更新bits2

???????? ??????? System.out.println("\nbits2 AND bits1: ");

???????? ??????? System.out.println(bits2);


???????? ??????? // OR bits

???????? ??????? bits2.or(bits1);????? // bits2和 bits1 相或,更新bits2

???????? ??????? System.out.println("\nbits2 OR bits1: ");

???????? ??????? System.out.println(bits2);


???????? ??????? // XOR bits

???????? ??????? bits2.xor(bits1);???? // bits2和 bits1 相異或,更新bits2

???????? ??????? System.out.println("\nbits2 XOR bits1: ");

???????? ??????? System.out.println(bits2);

???????? ?}

}

結(jié)果:

Initial pattern in bits1:

{0, 2, 4, 6, 8, 10, 12, 14}


Initial pattern in bits2:

{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}


bits2 AND bits1:

{2, 4, 6, 8, 12, 14}


bits2 OR bits1:

{0, 2, 4, 6, 8, 10, 12, 14}


bits2 XOR bits1:

{}





2、泛型

泛型提供了編譯時(shí)類型安全檢測機(jī)制,該機(jī)制允許程序員在編譯時(shí)檢測到非法的類型。

泛型的本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù)。

例:

package Test_hello;


public class Hello {


???????? ??? public static void main(String[] args) {??? //主函數(shù)

???????? ??? ???????? ?// 創(chuàng)建不同類型數(shù)組: Integer, Double 和 Character

???????? ??? ???????? GenericMethodTest GEN? = new GenericMethodTest();

???????? ??? ???????? Integer[] intArray = { 1, 2, 3, 4, 5 };

???????? ??????? Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };

???????? ??????? Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };


???????? ??????? System.out.println( "整型數(shù)組元素為:" );

???????? ??????? GEN.printArray(intArray); // 傳遞一個(gè)整型數(shù)組


???????? ??????? System.out.println( "\n雙精度型數(shù)組元素為:" );

???????? ??????? GEN.printArray(doubleArray); // 傳遞一個(gè)雙精度型數(shù)組


???????? ??????? System.out.println( "\n字符型數(shù)組元素為:" );

???????? ??????? GEN.printArray(charArray); // 傳遞一個(gè)字符型數(shù)組

???????? ?}

}


?class GenericMethodTest

{

?? // 泛型方法 printArray????????????????????????

?? public static < E > void printArray( E[] inputArray )

?? {

????? // 輸出數(shù)組元素???????????

???????? for ( E element : inputArray ){???????

??????????? System.out.printf( "%s ", element );

???????? }

???????? System.out.println();

??? }

}

結(jié)果:

整型數(shù)組元素為:

1 2 3 4 5


雙精度型數(shù)組元素為:

1.1 2.2 3.3 4.4


字符型數(shù)組元素為:

H E L L O

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容