.


:




:

































 

 

 

 


4

C#

 

1. : C#, .

 

# . System.Collections , . , , " " . , , , , -. #-.

, . . , ArrayList, Hashtable, Stack Queue, " ". , .

.NET Framework : , . , , . , /. object, .

. , , .

, , . , . , - BitArray , .

(), IEnumerator IEnumerable.

. IEnumerable, , IEnumerator. , , , . , , foreach. . ++-, , #- (Standard Template Library - STL), C++. , C++ , # . Java. Java- Collections Framework, #-.

 

 

. . . . 2.2.1 .

 

2.2.1

ArrayList , ..
Hashtabie - /
Queue , , :
SortedList /
Stack , , :

 

ArrayList

ArrayList , . # , . , , . , . ArrayList. ArrayList , . ArrayList . . . ArrayList, , , .

ArrayList ICollection, IList, IEnumerable ICloneable. ArrayList :

public ArrayList()

public ArrayList(ICollection )

public ArrayList(int capacity)

ArrayList- , 16 . , , . . ( ) . ArrayList- , , , .

, , ArrayList, . ArrayList Sort(). , BinarySearch(). ArrayList- , Reverse(). ArrayList , . , insertRange(), ArrayList- . RemoveRange() . , SetRange(). , . .

ArrayList . , Synchronized().

2.2.1.1 ArrayList.

 

2.2.1.1 - ArrayList

public virtual void AddRange(ICollection )
public virtual int BinarySearch (object v) , v. . , .
public virtual int BinarySearch(object v, IComparer comp) , v, , comp. . , .
public virtual int BinarySearch(int startldx, int count, object v, IComparer comp) , v, , comp. , startldx, count . . , .
public virtual void CopyTo(Array ar, int startldx) , , startldx, , .
public virtual void CopyTo(int srcldx, Array ar, int destldx, int count) count , , srcidx, , , , destldx.
public virtual ArrayList GetRange(int idx, int count) ArrayList. idx count . ,
public static ArrayList FixedSize(ArrayList ar) ar ArrayList-
public virtual void InsertRange(int startldx, ICollection c) , , , , startldx
public virtual int LastlndexOf(object v) v . , -1
public static ArrayList Readonly(ArrayList ar)
 
ar ArrayList-, ,

public virtual void ReraoveRange(int idx, int count) count , , idx
public virtual void Reverse()
public virtual void Reverse(int startldx, int count) count , startldx
public virtual void SetRange(int startldx, ICollection c) , startldx, ,
public virtual void Sort()
public virtual void Sort(IComparer comp) , comp. comp ,
public virtual void Sort (int startidx, int endidx, icomparer comp) , comp. startidx endidx. comp ,
public static ArrayList Synchronized(ArrayList list)
public virtual object [ ] ToArray () ,
public virtual Array ToArray (Type type) , . type
public virtual void TrimToSize() Capacity Count

 

ArrayList . ArrayList-, . . . "" .

 

// ArrayList-.

using System;

using System.Collections;

class ArrayListDemo {

public static void Main() {

// .

ArrayList al = new ArrayListO;

Console.WriteLine(" : " +

al.Capacity);

Console.WriteLine(" :

al.Count);

Console.WriteLine();

Console.WriteLine(" 6 .");

// .

al.Add(C);

al.Add(A);

al.Add(B);

al.Add(D);

al.Add(F);

Console.WriteLine(" : " + al.Capacity);

Console.WriteLine(" : " + al.Count);

// , .

Console.Write(" : ");

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

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

Console.WriteLine("\n");

Console.WriteLine(" 2 .");

// .

al.Remove(F);

al.Remove(A);

Console.WriteLine(" : " + al.Capacity);

Console.WriteLine(" : " + al.Count);

// foreach.

Console.Write(": ");

foreach(char in al)

Console.Write(c + " ");

Console.WriteLine("\n");

Console.WriteLine(" 20 .");

// ,

// .

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

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

Console.WriteLine(" : " +

al.Capacity);

Console.WriteLine(

" 20 : " + al.Count);

Console.Write(": ");

foreach(char in al)

Console.Write( + " ");

Console.WriteLine("\n");

// , .

Console.WriteLine(" .");

al[0] = X;

al[l] = Y';

al[2] = Z;

Console.Write(": ");

foreach(char in al)

Console.Write(c + " ");

Console.WriteLine();

}

}

 

:

 

: 16

: 0

6 .

: 16

: 6

: D F

2 .

: 16

: 4

: D

20 .

: 32

20 : 24

: C E B D a b c d e f g h i j k l m n o p q r s t

.

: X Y Z D a b c d e f g h i j k l m n o p q r s t

 

, , 16. , , , .

Hashtable

Hashtable , -. , , - , (hashing). , , -, . - , , . - , .. - . , , , , . Hashtable IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback ICloneable. Hashtable , ( ):

public Hashtable()

public Hashtable(IDictionary )

public Hashtable(int capacity)

public Hashtable(int capacity, float fillRatio)

 
Hashtable. Hashtable- c. - capacity, ( capacity), ( fillRatio). ( ), 0,1-1,0, -, . , , , . , , 1,0. Hashtable , , . 2.2.2.1. , Hashtable- , ContainsKey (). , - , ContainsValue (). Hashtable- IDictionaryEnumerator, GetEnumerator (). , , /, IDictionaryEnumerator.

 

2.2.2.1 Hashtable

public virtual bool ContainsKey (object k) true, Hashtable- , k. false
public virtual bool ContainsValue (object v) true, Hashtable- , v. false
public virtual IDictionaryEnumerator GetEnumerator() Hashtable- p IDictionaryEnumerator.
public static Hashtable Synchronized (Hashtable ht) Hashtable-, ht

 

Hashtable, , , public-. , Hashtable- :

public virtual ICollection Keys { get; }

public virtual ICollection Values { get; }

Hashtable , . Hashtable protected- Comparer, .

Hashtable / DictionaryEntry, , . , Hashtable- Add(), : .

, Hashtable- . , .

, Hashtable-:

 

// Hashtable-.

using System;

using System.Collections;

class HashtableDemo {

public static void Main() {

// -.

Hashtable ht = new Hashtable();

// -.

ht.Add("", " ");

ht.Add("", " ");

ht.Add("", " ");

ht.Add("", " ");

// ,

ht[""] = " ";

// *.

ICollection = ht.Keys;

// ,

foreach(string str in )

Console.WriteLine(str + ": " + ht[str]);

}

}

 

:

 

:

:

:

:

:

 

, / . , - ht. -, Keys. - ht, , . , , IDictionary Hashtable, .

 

SortedList

SortedList , / , . SortedList IDictionary, ICollection, IEnumerable ICloneable. SortedList , :

public SortedList()

public SortedList(IDictionary c)

public SortedList(int capacity)

public SortedList(IComparer comp)

, 16 . SortedList-, , . SortedList-, , capacity. , , . , . , 16 .

SortedList- , , . , , . SortedList- , . , , , .

SortedList , , . 2.2.3.1. , SortedList- , ContainsKey(). , , ContainsValue(). SortedList- IDictionaryEnumerator, GetEnumerator (). , , /, IDictionaryEnumerator. SortedList- Synchronized().

 

2.2.3.1 SortedList

public virtual bool ContainsKey(object k) true, SortedList- , k. false
public virtual bool ContainsValue(object v) true, SortedList- , k. false
public virtual object GetBylndex(int idx) , idx
public virtual IDictionaryEnumerator GetEnumerator() IDictionaryEnumerator SortedList-
public virtual object GetKeyUnt idx) , idx
public virtual iList GetKeyList() iList- , SortedList-
public virtual IList GetValueList() iList- , SortedList-
public virtual int IndexOfKey(object k) , . -1,
public virtual int IndexOfValue(object v) , v. -1,
public virtual void SetBylndex(int idx, object v) , idx, , v
public static SortedList Synchronized(SortedList sl) sortedList-, sl
public virtual void TrimToSize() capacity Count

 

. , , GetBylndex (), , , SetBylndex (). , , GetKey (). GetKeyList (), GetValueList (). IndexOf Key (), IndexOf Value (). SortedList , iDictionary, , . SortedList , , . , SortedList-, :

public virtual ICollection Keys { get; }

public virtual ICollection Values { get; }

SortedList-. Hashtable -, SortedList- / DictionaryEntry, , SortedList, . SortedList , , Hashtable-. , , SortedList- .

 

// SortedList-.

using System;

using System.Collections;

class SLDemo {

public static void Main() {

// SortedList.

SortedList si = new SortedList();

// .

sl.Add("", " ");

sl.Add("", " ");

sl.Add("", " ");

sl.Add("", " ");

// ,

sl[""] = " ";

// .

ICollection = sl.Keys;

// .

Console.WriteLine(

" , " +

".");

foreach(string str in )

Console.WriteLine(str + ": " + sl[str]);

Console.WriteLine();

// , .

Console.WriteLine(

" , " +

" .");

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

Console.WriteLine(sl.GetBylndex(i));

Console.WriteLine();

// .

Console.WriteLine(

" .");

foreach(string str in )

Console.WriteLine(str + ": " + sl.IndexOfKey(str));

}

}

 

:

 

, .

:

:

:

:

:

, .

.

: 0

: 1

: 2

: 3

: 4

Stack

, , , " " (last-in, first-out LIFO). , , . , , , .. . . , ( , ).

, , Stack. ICollection, IEnumerable ICloneable. , , , , , .

stack :

public Stack()

public Stack(int capacity)

public Stack(ICollection c)

, 10 . , capacity. , , . , , stack, , 2.2.4.1. , , Push(). , Pop(). Pop() , , InvalidOperationException. Peek() , .

 

2.2.4.1 Stack

public virtual bool Contains(object v) true, v . false
public virtual void Clear() count ,
public virtual object Peek() , ,
public virtual object Pop() , ,
public virtual void Push(object v) v
public static Stack Synchronized(Stack stk) stack-, stk
public virtual object[] ToArray() , -

 

: Integer, .

 

// Stack.

using System;

using System.Collections;

class StackDemo {

static void showPush(Stack st, int a) {

st.Push(a);

Console.WriteLine(

" : Push(" + + ")"

Console.Write(" : ");

foreach(int i in st)

Console.Write(i + " ");

Console.WriteLine();

static void showPop(Stack st) {

Console.Write(" : Pop -> ")

int a = (int) st.Pop();

Console.WriteLine(a);

Console.Write(" : ");

foreach(int i in st)

Console.Write(i + " ");

Console.WriteLine();

public static void Main() {

Stack st = new Stack();

foreach(int i in st)

Console.Write(i + " ");

Console.WriteLine();

showPush (st, 22);

showPush(st, 65);

showPush(st, 91);

showPop(st);

showPop(st);

showPop(st);

try {

showPop(st);

} catch (InvalidOperationException) {

Console.WriteLine(" .");

}

}

}

 

. , ( InvalidOperationException), ( ).

 

: Push(22)

: 22

: Push(65) ^

: 65 22

: Push(91)

: 91 65 22

: Pop -> 91

: 65 22

: Pop -> 65

: 22

: Pop -> 22

:

: Pop -> .

 

Queue

. " " (first-in, first-out FIFO). , , , . ? , , , , . , ( ) , Internet. .

, , Queue. iCollection, IEnumerable ICloneable. , , , , , , 2,0.

Queue :

public Queue()

public Queue (int capacity)

public Queue (int capacity, float growFact)

public Queue (ICollection c)

, 32 , 2,0. , capacity, 2,0. , growFact. , , .

, , Queue, , 2.2.5.1. . , Enqueue(). , Dequeue(). Dequeue() , , InvalidOperationException. () , .

 

2.2.5.1 - Queue

public virtual bool Contains (object v) true, v . false
public virtual void clear () Count ,
public virtual object Dequeue () ,
public virtual void Enqueue(object v) v
public virtual object Peek () ,
public static Queue Synchronized(Queue q) , g
public virtual ob j ect [ ] () ,
public virtual void TrimToSize() Capacity Count

 

, Queue:

 

// Queue.

using System;

using System.Collections;

class QueueDemo {

static void showEnq(Queue q, int a) {

q.Enqueue(a);

Console.WriteLine(

" : Enqueue(" + + ")");

Console.Write(" : ");

foreach(int i in q)

Console.Write(i + " ");

Console.WriteLine();

static void showDeq(Queue q) {

Console.Write(

" : Dequeue -> ");

int a = (int) q.Dequeue();

Console.WriteLine(a);

Console.Write(" : ");

foreach(int i in q)

Console.Write(i + " ");

Console.WriteLine();

public static void Main() {

Queue q = new Queue();

foreach(int i in q)

Console.Write(i + " ");

Console.WriteLine();

showEnq(q, 22);

showEnq(q, 65);

showEnq(q, 91);

showDeq(q);

showDeq(q);

showDeq(q);

try {

showDeq(q);

} catch (InvalidOperationException) {

Console.WriteLine(" .");

}

}

}

 

:

 

: Enqueue(22)

: 22

: Enqueue(65)

: 22 65

: Enqueue(91)

: 22 65 91

: Dequeue -> 22

: 65 91

: Dequeue -> 65

: 91

: Dequeue -> 91

:

: Dequeue -> .

 

. .

 

:

 

.

.

c .

 

  ArrayList
  ArrayList
  . . Hashtable
  . Hashtable
  . n SortedList
  . SortedList
  Stack
  . Stack
  FIFO Queue
  - Queue

 


 

1. . # .NET. . .: , 2007 796.:.

2. .. #. . .: -, 2007 432.: .

 



<== | ==>
 | SADT
:


: 2016-12-18; !; : 781 |


:

:

, .
==> ...

1669 - | 1488 -


© 2015-2024 lektsii.org - -

: 0.27 .