— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet528/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   524   525   526   527   528   529   530   531   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

generic 
types
Here’s how it works with regular 
arrays:


collections 
with 
generics
you are here
4
569
Using polymorphic arguments and generics
So we saw how the whole thing worked with arrays, but will it work 
the same way when we switch from an array to an ArrayList? Sounds 
reasonable, doesn’t it?
First, let’s try it with only the Animal ArrayList. We made just a few 
changes to the go() method:
public void go() {
ArrayList animals = new ArrayList();
animals.add(new Dog());
animals.add(new Cat());
animals.add(new Dog());
takeAnimals(animals);
}
public void 
takeAnimals(ArrayList animals)
{
for(Animal a: animals) {
a.eat();
}
}
A simple change from Animal[] to 
ArrayList
We have to add one at a time since there’s no 
shortcut syntax like there is for array creation.
This is the same code, except now the “animals” 
variable refers to an ArrayList instead of array.
The method now takes an ArrayList 
instead of an array, but everything else is 
the same. Remember, that for loop syntax 
works for both arrays and collections.
Passing in just ArrayList
%java TestGenerics2
animal eating
animal eating
animal eating
animal eating
animal eating
animal eating
File Edit Window Help CatFoodIsBetter
Compiles and runs just fine


570
 
chapter 16
But will it work with ArrayList ?
Because of polymorphism, the compiler let us pass a Dog array 
to a method with an Animal array argument. No problem. And 
an ArrayList can be passed to a method with an 
ArrayList argument. So the big question is, will the 
ArrayList argument accept an ArrayList? If it works 
with arrays, shouldn’t it work here too?
public void go() {
ArrayList animals = new ArrayList()
;
animals.add(new Dog());
animals.add(new Cat());
animals.add(new Dog());
takeAnimals(animals);

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   524   525   526   527   528   529   530   531   ...   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