天天看點

java非泛型集合_Java非泛型與泛型集合

與非泛型代碼相比,使用泛型的程式有很多好處。

代碼重用:使用泛型,隻需編寫一次方法/類/接口一次,即可用于任何類型。 在非泛型中,需要在需要時一次又一次地編寫代碼。

類型安全性:與在運作時相比,泛型在顯示編譯時會出錯(最好在編譯時知道代碼中的問題,而不是使代碼在運作時失敗)。

例如:建立一個存儲學生姓名的ArrayList,如果程式員錯誤地添加了一個整數對象而不是字元串,則編譯器允許它。 但是,當從ArrayList檢索此資料時,它将在運作時導緻非通用ArrayList出現問題。

// A Simple Java program to demonstrate that NOT using

// generics can cause run time exceptions

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList without any type specified

ArrayList al = new ArrayList();

al.add("One");

al.add("Two");

al.add(10); // Compiler allows this

String s1 = (String)al.get(0);

String s2 = (String)al.get(1);

try {

// Causes Runtime Exception

String s3 = (String)al.get(2);

}

catch (Exception e) {

System.out.println("Exception: " + e);

}

}

}

運作結果如下:

Exception:

java.lang.ClassCastException:

java.lang.Integer cannot be cast to java.lang.String

泛型如何解決此問題:如果将此清單設為泛型,則在任何其他情況下,它将僅使用String對象并引發編譯時錯誤。

// Using generics converts run time exceptions into

// compile time errors

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList with String specified

ArrayList al = new ArrayList();

al.add("Sachin");

al.add("Rahul");

// Now Compiler doesn't allow this

al.add(10);

String s1 = al.get(0);

String s2 = al.get(1);

String s3 = al.get(2);

}

}

運作結果時間:

15: error: no suitable method found for add(int)

al.add(10);

^

不需要單獨的類型轉換:如果不需要泛型,則在上面的示例中,每次要從ArrayList檢索資料時,都需要進行類型轉換。 在每個檢索操作中進行類型轉換都是一個很大的麻煩。 如果已經通過某種方式知道清單僅儲存字元串資料,則可以避免這種情況。

例如:

// A Simple Java program to demonstrate that

// type casting is needed everytime in Non-Generic

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList without any type specified

ArrayList al = new ArrayList();

al.add("Sachin");

al.add("Rahul");

// For every retrieval,

// it needs to be casted to String for use

String s1 = (String)al.get(0);

String s2 = (String)al.get(1);

}

}

泛型如何解決此問題:如果将此清單設為泛型,則它将僅使用String對象,并且在檢索時僅傳回String對象。 是以,将不需要單獨的類型轉換。

// A Simple Java program to demonstrate that

// type casting is not needed in Generic

import java.util.*;

class Test {

public static void main(String[] args)

{

// Creating an ArrayList with String specified

ArrayList al = new ArrayList();

al.add("One");

al.add("Two");

// Retrieval can be easily

// without the trouble of casting

String s1 = al.get(0);

String s2 = al.get(1);

}

}

實作泛型算法:通過使用泛型,人們可以實作适用于不同類型對象的算法,并且它們同樣是類型安全的。

泛型和非泛型的差別對比表如下:

比較項

非一般集合

一般集合

文法

ArrayList list = new ArrayList();

ArrayList list = new ArrayList();

類型安全

可以儲存任何類型的資料。 是以不是類型安全的。

隻能儲存定義的資料類型。 是以輸入安全。

類型轉換

每次檢索都需要進行個體類型轉換。

無需類型轉換。

編譯時檢查

在運作時檢查類型安全。

在編譯時檢查類型安全性。

歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動成果。

轉載請注明:文章轉載自 有差別網 [http://www.hasdiffer.com]

本文标題:Java非泛型與泛型集合的差別

本文連結:http://www.vsdiffer.com/non-generic-vs-generic-collection-in-java.html

免責聲明:本站部分内容除注明轉載外,均為本站網站使用者投稿或網際網路整理。對于該内容的正确性如何,本站不負任何責任。同時,如本網站内容無意之中冒犯了您的權益,請聯系站長,郵箱:1478761107#qq.com(使用@代替#),我們核實并會盡快處理。

相關主題

随機