Arrays und Listen
Imports System.Collections.Generic
Module Program
Sub Main()
Dim a As Integer() = {5, 3, 9, 1, 7}
Console.WriteLine(a.Length & " " & a(0) & " " & a(a.Length - 1))
a(0) = 50
Array.Sort(a)
Console.WriteLine(String.Join(", ", a))
Array.Reverse(a)
Console.WriteLine(String.Join(", ", a))
Dim m(1, 2) As Integer
m(1, 2) = 7
Console.WriteLine(m(1, 2) & " " & m.GetLength(0) & "x" & m.GetLength(1))
Dim l As New List(Of Integer) From {5, 3, 9}
l.Add(1)
l.Insert(0, 100)
l.Remove(3)
Console.WriteLine(String.Join(", ", l))
Console.WriteLine(l.Count & " " & l.Contains(9) & " " & l.IndexOf(9))
l.Sort()
Console.WriteLine(String.Join(", ", l))
l.Reverse()
Console.WriteLine(String.Join(", ", l))
For Each x In l
Console.Write(x & " ")
Next
Console.WriteLine()
Dim zahlen = Enumerable.Range(1, 5).ToList()
Console.WriteLine(String.Join(" ", zahlen))
ReDim Preserve a(7)
Console.WriteLine(a.Length)
End Sub
End ModuleAusgabe
5 5 7 1, 3, 7, 9, 50 50, 9, 7, 3, 1 7 2x3 100, 5, 9, 1 4 True 2 1, 5, 9, 100 100, 9, 5, 1 100 9 5 1 1 2 3 4 5 8
Dictionaries und Mengen
Imports System.Collections.Generic
Module Program
Sub Main()
Dim alter As New Dictionary(Of String, Integer) From {{"Mia", 17}, {"Tom", 25}, {"Zoe", 31}}
alter("Ben") = 12
alter.Add("Max", 40)
Console.WriteLine(alter("Mia") & " " & alter.Count & " " & alter.ContainsKey("Tom"))
Dim wert As Integer
If alter.TryGetValue("Nobody", wert) Then
Console.WriteLine("gefunden")
Else
Console.WriteLine("nicht gefunden")
End If
alter.Remove("Max")
For Each paar In alter.OrderBy(Function(p) p.Key)
Console.WriteLine(paar.Key & " = " & paar.Value)
Next
Dim zaehler As New Dictionary(Of Char, Integer)
For Each c In "mississippi"
If zaehler.ContainsKey(c) Then zaehler(c) += 1 Else zaehler(c) = 1
Next
Console.WriteLine(String.Join(" ", zaehler.OrderBy(Function(p) p.Key).Select(Function(p) p.Key & ":" & p.Value)))
Dim s As New HashSet(Of Integer) From {3, 1, 2, 3, 1}
s.Add(9)
Console.WriteLine(s.Count & " " & s.Contains(2))
Dim s2 As New HashSet(Of Integer) From {1, 9, 100}
s.IntersectWith(s2)
Console.WriteLine(String.Join(",", s.OrderBy(Function(x) x)))
Dim stapel As New Stack(Of Integer)
stapel.Push(1) : stapel.Push(2) : stapel.Push(3)
Console.WriteLine(stapel.Pop() & " " & stapel.Peek())
Dim schlange As New Queue(Of String)
schlange.Enqueue("a") : schlange.Enqueue("b")
Console.WriteLine(schlange.Dequeue() & " " & schlange.Count)
End Sub
End ModuleAusgabe
17 5 True nicht gefunden Ben = 12 Mia = 17 Tom = 25 Zoe = 31 i:4 m:1 p:2 s:4 4 True 1,9 3 2 a 1
LINQ
Mit LINQ (System.Linq) fragt man Sammlungen deklarativ ab:
Imports System.Linq
Module Program
Sub Main()
Dim l = {5, 3, 9, 1, 7}
Console.WriteLine(String.Join(" ", l.Select(Function(x) x * 2)))
Console.WriteLine(String.Join(" ", l.Where(Function(x) x > 4)))
Console.WriteLine(String.Join(" ", l.OrderBy(Function(x) x)))
Console.WriteLine(String.Join(" ", l.OrderByDescending(Function(x) x)))
Console.WriteLine(l.Sum() & " " & l.Min() & " " & l.Max() & " " & l.Average())
Console.WriteLine(l.Any(Function(x) x > 8) & " " & l.All(Function(x) x > 0))
Console.WriteLine(l.First() & " " & l.Last() & " " & l.FirstOrDefault(Function(x) x > 100))
Console.WriteLine(String.Join(" ", l.Take(2)) & " | " & String.Join(" ", l.Skip(3)))
Console.WriteLine(l.Count(Function(x) x Mod 2 = 1))
Console.WriteLine(String.Join(" ", l.Distinct()))
Console.WriteLine(l.Aggregate(Function(a, b) a + b))
Dim woerter = {"ein", "kleiner", "langer", "satz", "mit", "drei"}
For Each g In woerter.GroupBy(Function(w) w.Length).OrderBy(Function(x) x.Key)
Console.WriteLine(g.Key & ": " & String.Join(", ", g))
Next
Dim abfrage = From w In woerter
Where w.Length > 3
Order By w
Select w.ToUpper()
Console.WriteLine(String.Join(" ", abfrage))
Console.WriteLine(String.Join(" ", Enumerable.Range(1, 5).Select(Function(i) i * i)))
Console.WriteLine(String.Join(" ", {1, 2, 3}.Zip({"a", "b", "c"}, Function(n, s) n & s)))
End Sub
End ModuleAusgabe
10 6 18 2 14 5 9 7 1 3 5 7 9 9 7 5 3 1 25 1 9 5 True True 5 7 0 5 3 | 1 7 5 5 3 9 1 7 25 3: ein, mit 4: satz, drei 6: langer 7: kleiner DREI KLEINER LANGER SATZ 1 4 9 16 25 1a 2b 3c
Merke
- Arrays
{...}haben feste Größe,List(Of T)wächst;Dictionary,HashSet,Stack,Queue - Indexzugriff mit runden Klammern:
a(0) - LINQ-Methoden (
Select,Where,OrderBy,GroupBy) und Abfrageausdrücke (From ... Select) - Sammlungen mit
From {...}initialisieren
Aufgabe
Zähle mit einem Dictionary die Wörter eines Satzes und gib die drei häufigsten aus.