This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Create ArrayList and fill with some initial values



Download 21,46 Mb.
Pdf ko'rish
bet621/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   617   618   619   620   621   622   623   624   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Create ArrayList and fill with some initial values.

ArrayList carArList = new ArrayList();

carArList.AddRange(new Car[] { new Car("Fred", 90, 10),

new Car("Mary", 100, 50), new Car("MB", 190, 11)});



// Print out # of items in ArrayList.

Console.WriteLine("Items in carArList: {0}", carArList.Count);



// Print out current values.

foreach(Car c in carArList)

Console.WriteLine("Car pet name: {0}", c.PetName);

// Insert a new item.

Console.WriteLine("->Inserting new Car.");

carArList.Insert(2, new Car("TheNewCar", 0, 12));

Console.WriteLine("Items in carArList: {0}", carArList.Count);



// Get object array from ArrayList and print again.

object[] arrayOfCars = carArList.ToArray();

for(int i = 0; i < arrayOfCars.Length; i++)

{

Console.WriteLine("Car pet name: {0}",



((Car)arrayOfCars[i]).PetName);

}

}



C H A P T E R   1 0  

C O L L E C T I O N S   A N D   G E N E R I C S



313

8849CH10.qxd  9/25/07  4:17 PM  Page 313




Here you are making use of the AddRange() method to populate your ArrayList with an array of

Car types (which is basically a shorthand notation for calling Add() number of times). Once you

print out the number of items in the collection (as well as enumerate over each item to obtain the

pet name), you invoke Insert(). As you can see, Insert() allows you to plug a new item into the

ArrayList at a specified index.

Finally, notice the call to the ToArray() method, which returns an array of System.Object types

based on the contents of the original ArrayList. From this array, we loop over the items once again

using the array’s indexer syntax. If you call this method from within Main(), you will find the

ArrayList has indeed grown by one item to account for the new Car object.


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   617   618   619   620   621   622   623   624   ...   1830




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