C# 0 The Complete Reference


IList , is implemented by  List



Download 4,07 Mb.
Pdf ko'rish
bet970/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   966   967   968   969   970   971   972   973   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

    Bu sahifa navigatsiya:
  • List
IList

, is implemented by 



List

, as shown here:

public T this[int 

idx

] { get; set; }

It sets or gets the value of the element at the index specified by 

idx.

Here is a program the demonstrates 



List

. It reworks the first 



ArrayList

 program 

shown earlier in this chapter. The only changes necessary are to substitute the name 

List

 for 


ArrayList

 and to use the generic type parameters.

// Demonstrate List.

using System;

using System.Collections.Generic;

class GenListDemo {

  static void Main() {

    // Create a list.

    List lst = new List();

    Console.WriteLine("Initial number of elements: " +

                       lst.Count);

    Console.WriteLine();

    Console.WriteLine("Adding 6 elements");

    // Add elements to the list.

    lst.Add('C');

    lst.Add('A');

public void

    Sor t(int 



star tIdx

, int 


count

,

           IComparer 



comp

)

Sor ts a por tion of the collection using the specified 



comparison object. The sor t begins at 

star tIdx

 and 


runs for 

count

 elements



.

 If 


comp

 is null, the default 

comparer for each object is used.

public T[ ] ToArray( )

Returns an array that contains copies of the elements 

of the invoking object.

public void TrimExcess( )

Reduces the capacity of the invoking list so that it is 

no more than 10 percent greater than the number of 

elements that it currently holds.

T

ABLE


 24-14 

A Sampling of Methods Defi ned by 

List

(continued)

Method


Description

www.freepdf-books.com




782

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

    lst.Add('E');

    lst.Add('B');

    lst.Add('D');

    lst.Add('F');

    Console.WriteLine("Number of elements: " +

                       lst.Count);

    // Display the list using array indexing.

    Console.Write("Current contents: ");

    for(int i=0; i < lst.Count; i++)

      Console.Write(lst[i] + " ");

    Console.WriteLine("\n");

    Console.WriteLine("Removing 2 elements");

    // Remove elements from the list.

    lst.Remove('F');

    lst.Remove('A');

    Console.WriteLine("Number of elements: " +

                       lst.Count);

    // Use foreach loop to display the list.

    Console.Write("Contents: ");

    foreach(char c in lst)

      Console.Write(c + " ");

    Console.WriteLine("\n");

    Console.WriteLine("Adding 20 more elements");

    // Add enough elements to force lst to grow.

    for(int i=0; i < 20; i++)

      lst.Add((char)('a' + i));

    Console.WriteLine("Current capacity: " +

                       lst.Capacity);

    Console.WriteLine("Number of elements after adding 20: " +

                       lst.Count);

    Console.Write("Contents: ");

    foreach(char c in lst)

      Console.Write(c + " ");

    Console.WriteLine("\n");

    // Change contents using array indexing.

    Console.WriteLine("Change first three elements");

    lst[0] = 'X';

    lst[1] = 'Y';

    lst[2] = 'Z';

    Console.Write("Contents: ");

    foreach(char c in lst)

      Console.Write(c + " ");

    Console.WriteLine();

    // Because of generic type-safety,

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   966   967   968   969   970   971   972   973   ...   1096




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