Sie sind auf Seite 1von 48

sew htl krems

Collections
Dipl.-Ing. Msc. Paul Panhofer Bsc.

2. November 2020
sew htl krems

1 Datenstukturen
Glossar

2 Collection
Arrays
Listen
Dictionary
Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


2/48
sew htl krems

Datenstrukturen

Eine Datenstruktur ist ein Objekt mit einer vorgegebener


Struktur, das zur Speicherung und Organisation von Daten
eingesetzt wird.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


3/48
sew htl krems

Datenstrukturen
Collection

Collection sind Datenstrukturen in der


System.Collections.Generic Library gekapselt werden.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


4/48
sew htl krems

Datenstrukturen
foreach Schleife

public class ArrayTest{


[Test]
public void TestArrayCollection(){
List<int> numbers = new List<int>(){
5, 3, 7, 8
};

foreach(int n in numbers){
Console.WriteLine(n);
}
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


5/48
sew htl krems

Datenstrukturen
Indexer

public class GameEngine {

private int[] playerIDs = new int[16];

public int this[int i]{


get { return playerIds[i]; }
set { playerIds[i] = value; }
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


6/48
sew htl krems

Datenstrukturen
Indexer

public class GameEngineTest {


[Test]
public void TestIndexer(){
GameEngine gm = new GameEngine();

gm[0] = 34;
gm[1] = 54;

Assert.AreEqual(34, gm[0]);
Assert.AreEqual(54, gm[1]);
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


7/48
sew htl krems

1 Datenstukturen
Glossar

2 Collection
Arrays
Listen
Dictionary
Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


8/48
sew htl krems

Collection
Array

Datenstruktur - Array
Arrays sind Datentypen, die zur Speicherung mehrer
Werte eines einzelnen Typs verwendet werden.

Das Array selbt ist ein Objekt und wird mit den new
Operator erzeugt.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


9/48
sew htl krems

Objekte und Klassen

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


10/48
sew htl krems

Collection - Array
Einfache Datentpyen

public class ArrayTest{


[Test]
public void TestArrayCollection(){
int[] numbers = new int[3];

numbers[0] = 4;
numbers[1] = 3;
numbers[2] = 1;

Assert.AreEqual(3, numbers.Length)
Assert.AreEqual(4, numbers[0]);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


11/48
sew htl krems

Collection - Array
Einfache Datentypen

public class ArrayTest{


[Test]
public void TestArrayCollection(){
int[] numbers = new int[] {
4, 3, 1
};

Assert.AreEqual(3, numbers.Length);
Assert.AreEqual(4, numbers[0]);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


12/48
sew htl krems

Collection - Array
Objekte
public class Point {

private int _x, _y;

public X { get; set; }


public Y { get; set; }

public Point(int x, int y){


this._x = x;
this._y = y;
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


13/48
sew htl krems

Collection - Array
Objekte

public class ArrayTest{


[Test]
public void TestArrayCollection(){
Point[] points = new Point[3];

points[0] = new Point(0,0);


points[1] = new Point(3,5);
points[2] = new Point(7,2);

Assert.AreEqual(3, points.Length);
Assert.AreEqual(5, points[1].Y);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


14/48
sew htl krems

Collection - Array
Objekte

public class ArrayTest{


[Test]
public void TestArrayCollection(){
Point[] points = new Point[] {
new Point(0,0),
new Point(3,5),
new Point(7,2)
};

Assert.AreEqual(3, points.Length);
Assert.AreEqual(5, points[1].Y);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


15/48
sew htl krems

Collection - Array
Mehrdimensionale Arrays
public class ArrayTest{
[Test]
public void TestArrayCollection(){
int[,] board = new int[3,3];
int counter = 0;

for(int y = 0; y < board.GetLength(0), y++)


for(int x = 0; x < board.GetLength(1), x++){
board[y, x] = couonter++;
}
}

Assert.AreEqual(3, board[0, 2]);


}
}
Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020
16/48
sew htl krems

1 Datenstukturen
Glossar

2 Collection
Arrays
Listen
Dictionary
Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


17/48
sew htl krems

Collection
Dynamische Datenstruktur: Listen

Datenstruktur: Listen
Listen sind dynamische Datenstruktur, die zur Speicherung
mehrerer Werten eines einzelnen Typs verwendet werden.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


18/48
sew htl krems

Collection - Listen
Einfache Datentpyen

public class ListTest{


[Test]
public void TestListCollection(){
List<int> numbers = new List<int>();

numbers.Add(3);
numbers.Add(2);
numbers.Add(5);

Assert.AreEqual(3, numbers.Count)
Assert.AreEqual(3, numbers[0]);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


19/48
sew htl krems

Collection - Listen
Komplexe Datentypen

public class ListTest{


[Test]
public void TestListCollection(){
List<Point> points = new List<Point>(){
new Point(3,2), new Point(9,1)
};

points.Add(new Point(11, 3));

Assert.AreEqual(3, points.Count);
Assert.AreEqual(9, points[1].X);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


20/48
sew htl krems

Listen
Methoden

Befehl Beschreibung
Add Fügt ein Element am Ende der
Liste ein.
AddRange Fügt eine Liste von Elementen
am Ende der Liste an.
Clear Löschte alle Elemente der Liste.
Contains Überprüft ob ein bestimmtes Ele-
ment in der Liste enthalten ist.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


21/48
sew htl krems

Listen
Methoden

Befehl Beschreibung
Insert Fügt ein Element an einem be-
stimmten Index ein.
InsertRange Fügt die Elemente einer anderer
Kollection ab einem bestimmten
Index an.
Remove Entfernt das erste Vorkommen
des angegeben Elements aus
der Liste.
RemoveAt Entfernt das Element am ange-
gebenem Index

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


22/48
sew htl krems

Listen
Methode: AddRange

public class ListTest{


[Test]
public void TestListMethod(){
List<Point> points = new List<Point>(){
new Point(3,4), new Point(7,1)
};

List<Point> c = new List<Point>();


c.AddRange(points);

Assert.AreEqual(2, c.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


23/48
sew htl krems

Listen
Methode: Clear
public class ListTest{
[Test]
public void TestListMethod(){
List<Point> points = new List<Point>(){
new Point(3,4), new Point(7,1),
new Point(4,4), new Point(5,2)
};

Assert.AreEqual(4, points.Count);
points.Clear();

Assert.AreEqual(0, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


24/48
sew htl krems

Listen
Methode: Contains

public class ListTest{


[Test]
public void TestListMethod(){
List<Point> points = new List<Point>(){
new Point(3,4), new Point(7,1),
new Point(4,4), new Point(5,2)
};

Assert.True(
points.Contains( new Point(4,4) )
);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


25/48
sew htl krems

Listen
Methode: Insert
public class ListTest{
[Test]
public void TestListMethod(){
List<Point> points = new List<Point>(){
new Point(3,4), new Point(7,1),
new Point(4,4), new Point(5,2)
};

Assert.AreEqual(7, points[1].X);
points.Insert(1, new Point(4,1));

Assert.AreEqual(4, points[1].X);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


26/48
sew htl krems

Listen
Methode: Remove

public class ListTest{


[Test]
public void TestListMethod(){
List<Point> points = new List<Point>(){
new Point(3,4), new Point(7,1),
new Point(4,4), new Point(5,2)
};

points.Remove(new Point(4,4));

Assert.AreEqual(3, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


27/48
sew htl krems

1 Datenstukturen
Glossar

2 Collection
Arrays
Listen
Dictionary
Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


28/48
sew htl krems

Collection
Dynamische Datenstruktur: Dictionary

Datenstruktur: Dictionary
Ein Dictionary ist eine dynamische Datenstruktur, zur
Verwaltung von Schlüssel Werte Paaren.

Ein Dictionary zeigt dabei dasselbe Verhalten wie ein


Array. Im Gegensatz zu einem Array mus der Index eines
Dictionaries jedoch kein numerischer Wert sein.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


29/48
sew htl krems

Dynamische Datenstruktur:
Dictionary
public class DictionaryTest{
[Test]
public void TestDictionary(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

Assert.AreEqual("0664/89723",
phoneList["Haidvogl"]);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


30/48
sew htl krems

Dictionary
Methode: Count, Clear
public class DictionaryTest{
[Test]
public void TestDictionryMethod(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

Assert.AreEqual(2, phoneList.Count);
phoneList.Clear();

Assert.AreEqual(0, phoneList.Count);
}
}
Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020
31/48
sew htl krems

Dictionary
Methode: ContainsKey, ContainsValue

public class DictionaryTest{


[Test]
public void TestDictionryMethod(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

Assert.True(phoneList.ContainsKey("Panhofer"));
Assert.True(phoneList.ContainsValue("0650/98376"));
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


32/48
sew htl krems

Dictionary
Methode: Remove

public class DictionaryTest{


[Test]
public void TestDictionryMethod(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

phoneList.Remove("Panhofer");
Assert.AreEqual(2, phoneList.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


33/48
sew htl krems

Dictionary
forEach Schleife
public class DictionaryTest{
[Test]
public void TestDictionryMethod(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

foreach(var name in phoneList.Keys()){


Console.Writeln(phoneList[name]);
}
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


34/48
sew htl krems

Dictionary
forEach Schleife
public class DictionaryTest{
[Test]
public void TestDictionryMethod(){
Dictionary<String, String> phoneList = new
Dictionary<String, String>();

phoneList["Haidvogl"] = "0664/89723";
phoneList["Panhofer"] = "0650/98376";

foreach(var phoneNr in phoneList.Values()){


Console.Writeln(phoneNr);
}
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


35/48
sew htl krems

1 Datenstukturen
Glossar

2 Collection
Arrays
Listen
Dictionary
Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


36/48
sew htl krems

Collection
Dynamische Datenstruktur: Stack

Datenstruktur: Stack
Ein Stack ist eine Dynamische Datenstruktur der Elemente
nach dem LIFO Prinzip verwaltet. Die Elemente des Stacks
werden dabei übereinander gestapelt und können nur in
umgekehrter Reihenfolge von Stack ausgelesen werden.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


37/48
sew htl krems

Collection: Stack

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


38/48
sew htl krems

Collection
Dynamische Datenstruktur: Stack

Befehl Beschreibung
Push Die Methode fügt ein Element zum Stack
hinzu. Dazu wird das Element oben auf den
Stack gelegt.
Pop Die Methode entfernt das oberste Objekt
vom Stapel.
Peek Die Methode gibt eine Referenz auf das
oberste Element im Stapel zurück.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


39/48
sew htl krems

Collection
Dynamische Datenstruktur: Stack

Befehl Beschreibung
Clear Die Methode loescht alle Objektereferen-
zen aus dem Stack.
Contains Die Methode prueft ob ein bestimmtes Ele-
ment in der Liste enthalten ist.
ToArray Kopiert die im Stack verwalteten Objektre-
ferenzen in ein Array.
CopyTo Kopiert die im Stack verwalteten Objektre-
ferenzen in ein Array. Es werden alle Ele-
mente ab einem bestimmten Index kopiert.

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


40/48
sew htl krems

Collection - Stack
Methode: Push

public class CollectionTest{


[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.AreEqual(2, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


41/48
sew htl krems

Collection - Stack
Methode: Pop
public class CollectionTest{
[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.AreEqual(2, points.Count);
Assert.AreEqual(9, (points.Pop()).X);

Assert.AreEqual(1, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


42/48
sew htl krems

Collection - Stack
Methode: Peek
public class CollectionTest{
[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.AreEqual(2, points.Count);
Assert.AreEqual(9, (points.Peek()).X);

Assert.AreEqual(2, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


43/48
sew htl krems

Collection - Stack
Methode: Peek
public class CollectionTest{
[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.AreEqual(2, points.Count);
Assert.AreEqual(9, (points.Peek()).X);

Assert.AreEqual(2, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


44/48
sew htl krems

Collection - Stack
Methode: Clear
public class CollectionTest{
[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.AreEqual(2, points.Count);
points.Clear();

Assert.AreEqual(0, points.Count);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


45/48
sew htl krems

Collection - Stack
Methode: Contains

public class CollectionTest{


[Test]
public void TestCollectionMethod(){
Stack<Point> points = new Stack<Point>();

points.Push(new Point(4,5));
points.Push(new Point(9,2));

Assert.True(points.Contains(new Point(9,2)));
Assert.False(points.Contains(new Point(2,2)));
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


46/48
sew htl krems

Collection - Stack
Methode: ToArray
public class CollectionTest{
[Test]
public void TestCollectionMethod(){
Stack<Point> pointStack = new Stack<Point>();

pointStack.Push(new Point(4,5));
pointStack.Push(new Point(9,2));

Point[] points = pointStack.ToArray();

Assert.AreEqual(2, points.Length);
Assert.AreEqual(2, points[1].Y);
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


47/48
sew htl krems

Collection - Stack
Methode: CopyTo

public class CollectionTest{


[Test]
public void TestCollectionMethod(){
Stack<Point> pointStack = new Stack<Point>();

pointStack.Push(new Point(4,5));
pointStack.Push(new Point(9,2));

Point[] points = new Point[pointStack.Count];

pointStack.CopyTo(points, 0)
}
}

Dipl.-Ing. Msc. Paul Panhofer Bsc. — SEW — 2. November 2020


48/48