Effective Java


// List-based Chooser - typesafe



Download 2,19 Mb.
Pdf ko'rish
bet119/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   115   116   117   118   119   120   121   122   ...   341
Bog'liq
Effective Java

// List-based Chooser - typesafe
public class Chooser {
private final 
List
choiceList;
public Chooser(Collection choices) {
choiceList = 
new ArrayList<>(choices)
;
}
public T choose() {
Random rnd = ThreadLocalRandom.current();
return 
choiceList.get(rnd.nextInt(choiceList.size()))
;
}
}
This version is a tad more verbose, and perhaps a tad slower, but it’s worth it for
the peace of mind that you won’t get a 
ClassCastException
at runtime. 
In summary, arrays and generics have very different type rules. Arrays are
covariant and reified; generics are invariant and erased. As a consequence, arrays
provide runtime type safety but not compile-time type safety, and vice versa for
generics. As a rule, arrays and generics don’t mix well. If you find yourself
mixing them and getting compile-time errors or warnings, your first impulse
should be to replace the arrays with lists.


CHAPTER 5
GENERICS
130
Item 29: Favor generic types
It is generally not too difficult to parameterize your declarations and make use of
the generic types and methods provided by the JDK. Writing your own generic
types is a bit more difficult, but it’s worth the effort to learn how.
Consider the simple (toy) stack implementation from Item 7:
Object-based_collection_-_a_prime_candidate_for_generics_public_class_Stack_{_private_Object'>// Object-based collection - a prime candidate for generics
public class Stack {
private 
Object
[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
elements = new 
Object
[DEFAULT_INITIAL_CAPACITY];
}
public void push(
Object
e) {
ensureCapacity();
elements[size++] = e;
}
public 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   115   116   117   118   119   120   121   122   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish