Effective Java


// Chooser - a class badly in need of generics!



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

// Chooser - a class badly in need of generics!
public class Chooser {
private final Object[] choiceArray;
public Chooser(Collection choices) {
choiceArray = choices.toArray();
}
public Object choose() {
Random rnd = ThreadLocalRandom.current();
return choiceArray[rnd.nextInt(choiceArray.length)];
}
}
To use this class, you have to cast the 
choose
method’s return value from
Object
to the desired type every time you use invoke the method, and the cast will
fail at runtime if you get the type wrong. Taking the advice of Item 29 to heart, we
attempt to modify 
Chooser
to make it generic. Changes are shown in boldface:
// A first cut at making Chooser generic - won't compile
public class Chooser

{
private final 
T
[] choiceArray;
public Chooser(Collection

choices) {
choiceArray = choices.toArray();
}
// choose method unchanged
}
If you try to compile this class, you’ll get this error message:
Chooser.java:9: error: incompatible types: Object[] cannot be 
converted to T[]
choiceArray = choices.toArray();
^
where T is a type-variable:
T extends Object declared in class Chooser


ITEM 28: PREFER LISTS TO ARRAYS
129
No big deal, you say, I’ll cast the 
Object
array to a 
T
array:
choiceArray = (T[]) choices.toArray();
This gets rid of the error, but instead you get a warning:
Chooser.java:9: warning: [unchecked] unchecked cast
choiceArray = (T[]) choices.toArray();
^
required: T[], found: Object[]
where T is a type-variable:
T extends Object declared in class Chooser
The compiler is telling you that it can’t vouch for the safety of the cast at runtime
because the program won’t know what type 
T
represents—remember, element
type information is erased from generics at runtime. Will the program work? Yes,
but the compiler can’t prove it. You could prove it to yourself, put the proof in a
comment and suppress the warning with an annotation, but you’re better off
eliminating the cause of warning (Item 27).
To eliminate the unchecked cast warning, use a list instead of an array. Here is
a version of the 
Chooser
class that compiles without error or warning:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   114   115   116   117   118   119   120   121   ...   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