Documentos de Académico
Documentos de Profesional
Documentos de Cultura
C# - Arreglos y Colecciones PDF
C# - Arreglos y Colecciones PDF
Arreglos y Colecciones
Por: scar Lpez, M.Sc.
olopez@uniandino.com.co
ARREGLOS
Arreglos
Arreglos - Declaracin
[[atributos]] [modificadores] tipo[] nombre;
Clase System.Array
Mtodo o Propiedad
Descripcin
IsFixedSize
Length
Rank
Clear()
Copy()
IndexOf()
LastIndexOf()
Reverse()
Sort()
4.
5.
6.
7.
8.
9.
10. }
Array - Ejemplo
1.
using System;
2.
3.
4.
5.
PrintMyArray(myArray);
Array.Reverse(myArray);
PrintMyArray(myArray);
6.
7.
String[] myOtherArray =
{"We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident"};
8.
9.
PrintMyArray(myOtherArray);
Array.Sort(myOtherArray);
PrintMyArray(myOtherArray);
Console.ReadLine();
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
}
OALP-2004 All Rights Reserved
Arreglos Multidimensionales
Arreglos Rectangulares
[[atributos]] [modificadores] tipo[,] nombre;
using System;
2.
class Tester {
public void Run() {
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
Arreglos Serrados
[[atributos]] [modificadores] tipo[][] nombre;
using System;
2.
class Tester {
public void Run() {
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
for(int
for(int
for(int
for(int
22.
23.
24.
25.
i=0;i<rowZero;i++)
i=0;i<rowOne;i++)
i=0;i<rowTwo;i++)
i=0;i<rowThree;i++)
26.
27.
28.
29.
30.
31.
Console.WriteLine("[0][{0}]
Console.WriteLine("[1][{0}]
Console.WriteLine("[2][{0}]
Console.WriteLine("[3][{0}]
=
=
=
=
{1}",i,jaggedArray[0][i]);
{1}",i,jaggedArray[1][i]);
{1}",i,jaggedArray[2][i]);
{1}",i,jaggedArray[3][i]);
COLECCIONES
Colecciones
Interfaces tiles
Interfaz
Mtodos
ICollection
IComparer
IEnumerable
IEnumerator GetEnumerator()
IEnumerator
IHashCodeProvider
Interfaces tiles
Interfaz
Mtodos
IList
IDictionary
IComparer e IComparable
IEnumerable e IEnumerator
using System;
using System.Collections;
3.
class TestForeach {
1.
4.
6.
string s = "abc123";
Console.WriteLine(s is IEnumerable + "\n");
7.
8.
Console.WriteLine("\n---\n");
9.
13.
IEnumerator ie = s.GetEnumerator();
while (ie.MoveNext()) {
char c = (char) ie.Current;
Console.WriteLine(c);
}
14.
Console.ReadLine();
5.
10.
11.
12.
15.
16.
IDictionary e IDictionaryEnumerator
Clase System.Collections.ArrayList
Mtodo o Propiedad
Descripcin
Capacity
Count
Item
Add()
Clear()
GetEnumerator()
Retorna el enumerador
Insert()
RemoveAt()
Reverse()
Sort()
ToArray()
ArrayList - Ejemplo
2.
using System;
using System.Collections;
3.
1.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
Clase System.Collections.Queue
Mtodo o Propiedad
Descripcin
Count
Clear()
Contains()
CopyTo()
Dequeue()
Enqueue()
GetEnumerator()
Retorna el enumerador
Peek()
ToArray()
Queue - Ejemplo
2.
using System;
using System.Collections;
3.
1.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
Clase System.Collections.Stack
Mtodo o Propiedad
Descripcin
Count
Clear()
Contains()
CopyTo()
GetEnumerator()
Retorna el enumerador
Peek()
Pop()
Push()
ToArray()
Stack - Ejemplo
2.
using System;
using System.Collections;
3.
class Tester {
1.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
Clase System.Collections.Hashtable
Mtodo o Propiedad
Descripcin
Count
Keys
Values
Item
Add()
Clear()
ContainsKey()
ContainsValue()
CopyTo()
GetEnumerator()
Retorna el enumerador
Remove()
Hashtable
Hashtable - Ejemplo
2.
using System;
using System.Collections;
3.
1.
4.
5.
6.
// Usando Add()
ht.Add("A", "apple"); ht.Add("D", "durian");
ht.Add("B", "banana"); ht.Add("C", "coconut");
PrintCollection(ht);
// Usando la propiedad Count
Console.WriteLine("Count: {0}\n", ht.Count);
// Usando ContainsKey()
Console.WriteLine(ht.ContainsKey("D"));
Console.WriteLine(ht.ContainsKey("S") + "\n");
// Usando ContainsValue()
Console.WriteLine(ht.ContainsValue("banana"));
Console.WriteLine(ht.ContainsValue("starfruit") + "\n");
// Usando Remove()
ht.Remove("B");
PrintCollection(ht);
// Usando la propiedad Keys
foreach (string key in ht.Keys) Console.WriteLine(key);
// Usando la propiedad Values()
Console.WriteLine();
foreach (string val in ht.Values) Console.WriteLine(val);
Console.ReadLine();
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.