— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet501/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   497   498   499   500   501   502   503   504   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

generic 
classes


collections 
with 
generics
you are here
4
543
public boolean add(
E
 o)
// more code
public class ArrayList

 extends AbstractList

 ... {


}

Using type parameters with ArrayList


THIS code:
Means ArrayList:
ArrayList
 
thisList = new ArrayList

public boolean add(
String
 o)
// more code
}

Is treated by the compiler as:


public class ArrayList

 extends AbstractList

... {


 o)
 extends AbstractList
In other words, the “E” is replaced by the real type (also called the type parameter
that you use when you create the ArrayList. And that’s why the add() method 
for ArrayList won’t let you add anything except objects of a reference type that’s 
compatible with the type of “E”. So if you make an ArrayList, the add() 
method suddenly becomes add(String o). If you make the ArrayList of type Dog
suddenly the add() method becomes add(Dog o).
Q: 
Is “E” the only thing you can put there? Because the docs for sort used “T”....
A: 
You can use anything that’s a legal Java identifier. That means anything that you 
could use for a method or variable name will work as a type parameter. But the conven-
tion is to use a single letter (so that’s what you should use), and a further convention is to 
use “T” unless you’re specifically writing a collection class, where you’d use “E” to repre-
sent the “type of the Element the collection will hold”.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   497   498   499   500   501   502   503   504   ...   555




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