Drop links or images here to add them to the editor.

H8c Collections — oplossingen oefeningen


Opwarmer 1 — Dierenlijst

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> dieren = new List<string>() { "Hond", "Kat", "Konijn", "Hamster", "Vis" };

            foreach (string dier in dieren)
            {
                Console.WriteLine(dier);
            }

            Console.WriteLine($"Er zijn {dieren.Count} dieren in de lijst.");
        }
    }
}

Opwarmer 2 — Boodschappenlijst

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> boodschappen = new List<string>() { "Brood", "Eieren", "Kaas" };

            boodschappen.Add("Boter");
            boodschappen.Insert(0, "Melk");

            for (int i = 0; i < boodschappen.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {boodschappen[i]}");
            }
        }
    }
}

Opwarmer 3 — Getallen verwijderen

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<int> getallen = new List<int>() { 10, 25, 7, 42, 18, 3 };

            foreach (int getal in getallen)
            {
                Console.WriteLine(getal);
            }

            Console.WriteLine("Welk getal wil je verwijderen?");
            int invoer = Convert.ToInt32(Console.ReadLine());

            if (getallen.Contains(invoer))
            {
                getallen.Remove(invoer);
                Console.WriteLine($"Het getal {invoer} is verwijderd.");
            }
            else
            {
                Console.WriteLine($"Het getal {invoer} staat niet in de lijst.");
            }

            foreach (int getal in getallen)
            {
                Console.WriteLine(getal);
            }

            Console.WriteLine($"Aantal getallen: {getallen.Count}");
        }
    }
}

Star Wars 0 — List declareren en initialiseren

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back"
            };

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Star Wars 1 — Add

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back"
            };

            starWarsFilms.Add("Star Wars Episode VI: Return Of The Jedi");

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Star Wars 2 — Insert prequels

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back",
                "Star Wars Episode VI: Return Of The Jedi"
            };

            starWarsFilms.Insert(0, "Star Wars Episode I: The Phantom Menace");
            starWarsFilms.Insert(1, "Star Wars Episode II: Attack of the Clones");
            starWarsFilms.Insert(2, "Star Wars Episode III: Revenge of the Sith");

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Star Wars 3 — Add en Insert

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode I: The Phantom Menace",
                "Star Wars Episode II: Attack of the Clones",
                "Star Wars Episode III: Revenge of the Sith",
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back",
                "Star Wars Episode VI: Return Of The Jedi"
            };

            starWarsFilms.Add("Star Wars Episode VII: The Force Awakens");
            starWarsFilms.Insert(3, "Rogue One");

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Star Wars 4 — Add en Insert vervolg

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode I: The Phantom Menace",
                "Star Wars Episode II: Attack of the Clones",
                "Star Wars Episode III: Revenge of the Sith",
                "Rogue One",
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back",
                "Star Wars Episode VI: Return Of The Jedi",
                "Star Wars Episode VII: The Force Awakens"
            };

            starWarsFilms.Add("Star Wars Episode VIII: The Last Jedi");
            starWarsFilms.Add("Star Wars Episode IX: The Rise of Skywalker");
            starWarsFilms.Insert(3, "Solo");

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Star Wars 5 — for, foreach en Reverse

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> starWarsFilms = new List<string>()
            {
                "Star Wars Episode I: The Phantom Menace",
                "Star Wars Episode II: Attack of the Clones",
                "Star Wars Episode III: Revenge of the Sith",
                "Solo",
                "Rogue One",
                "Star Wars Episode IV: A New Hope",
                "Star Wars Episode V: The Empire Strikes Back",
                "Star Wars Episode VI: Return Of The Jedi",
                "Star Wars Episode VII: The Force Awakens",
                "Star Wars Episode VIII: The Last Jedi",
                "Star Wars Episode IX: The Rise of Skywalker"
            };

            for (int i = 0; i < starWarsFilms.Count; i++)
            {
                Console.WriteLine(starWarsFilms[i]);
            }

            starWarsFilms.Reverse();

            foreach (string film in starWarsFilms)
            {
                Console.WriteLine(film);
            }
        }
    }
}

Drillen 1 — Declareren en initialiseren

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> fruitlijst = new List<string>() { "Appel", "Banaan", "Kers" };
            List<int> getallenlijst = new List<int>() { 10, 20, 30, 40 };

            Console.WriteLine("Fruitlijst:");
            foreach (string fruit in fruitlijst)
            {
                Console.WriteLine(fruit);
            }

            Console.WriteLine("Getallenlijst:");
            foreach (int getal in getallenlijst)
            {
                Console.WriteLine(getal);
            }
        }
    }
}

Drillen 2 — Alleen Add

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> films = new List<string>();

            string invoer;
            do
            {
                Console.WriteLine("Geef een film in (of STOP om te stoppen):");
                invoer = Console.ReadLine();
                if (invoer.ToUpper() != "STOP")
                {
                    films.Add(invoer);
                }
            } while (invoer.ToUpper() != "STOP");

            Console.WriteLine("Jouw filmlijst:");
            for (int i = 0; i < films.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {films[i]}");
            }
        }
    }
}

Drillen 3 — Alleen Insert

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> weekdagen = new List<string>() { "Maandag", "Woensdag", "Vrijdag" };

            Console.WriteLine("Originele lijst:");
            for (int i = 0; i < weekdagen.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {weekdagen[i]}");
            }

            weekdagen.Insert(1, "Dinsdag");
            weekdagen.Insert(3, "Donderdag");

            Console.WriteLine("Na Insert():");
            for (int i = 0; i < weekdagen.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {weekdagen[i]}");
            }
        }
    }
}

Drillen 4 — Alleen RemoveAt

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> kleuren = new List<string>() { "Rood", "Oranje", "Geel", "Groen", "Blauw" };

            for (int i = 0; i < kleuren.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {kleuren[i]}");
            }

            Console.WriteLine("Welk volgnummer wil je verwijderen?");
            int volgnummer = Convert.ToInt32(Console.ReadLine());
            kleuren.RemoveAt(volgnummer - 1);

            Console.WriteLine("Lijst na verwijdering:");
            for (int i = 0; i < kleuren.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {kleuren[i]}");
            }
        }
    }
}

Drillen 5 — Sort en Reverse

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> namen = new List<string>();

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Geef een naam in:");
                namen.Add(Console.ReadLine());
            }

            List<string> origineel = new List<string>(namen);

            Console.WriteLine("Originele volgorde:");
            foreach (string naam in origineel)
            {
                Console.WriteLine(naam);
            }

            namen.Sort();

            Console.WriteLine("Gesorteerd (A → Z):");
            foreach (string naam in namen)
            {
                Console.WriteLine(naam);
            }

            namen.Reverse();

            Console.WriteLine("Omgekeerd (Z → A):");
            foreach (string naam in namen)
            {
                Console.WriteLine(naam);
            }
        }
    }
}

Drillen 6 — Klaslijst invullen

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> leerlingen = new List<string>();

            string invoer;
            do
            {
                Console.WriteLine("Geef een naam in (of STOP om te stoppen):");
                invoer = Console.ReadLine();
                if (invoer.ToUpper() != "STOP")
                {
                    leerlingen.Add(invoer);
                }
            } while (invoer.ToUpper() != "STOP");

            Console.WriteLine($"Er zijn {leerlingen.Count} leerlingen in de klas:");

            for (int i = 0; i < leerlingen.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {leerlingen[i]}");
            }
        }
    }
}

Drillen 7 — Getallen optellen

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<int> getallen = new List<int>();

            int invoer;
            do
            {
                Console.WriteLine("Geef een getal in (0 om te stoppen):");
                invoer = Convert.ToInt32(Console.ReadLine());
                if (invoer != 0)
                {
                    getallen.Add(invoer);
                }
            } while (invoer != 0);

            Console.WriteLine("Jouw getallen:");
            foreach (int getal in getallen)
            {
                Console.WriteLine(getal);
            }

            Console.WriteLine($"Aantal: {getallen.Count}");

            int som = 0;
            foreach (int getal in getallen)
            {
                som += getal;
            }
            Console.WriteLine($"Som: {som}");
        }
    }
}

Drillen 8 — Zoeken en vinden

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> steden = new List<string>() { "Brussel", "Antwerpen", "Gent", "Brugge", "Leuven" };

            steden.Sort();

            Console.WriteLine("Steden in de lijst (gesorteerd):");
            for (int i = 0; i < steden.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {steden[i]}");
            }

            Console.WriteLine("Geef een stad om te zoeken:");
            string zoekStad = Console.ReadLine();

            if (steden.Contains(zoekStad))
            {
                int positie = steden.IndexOf(zoekStad) + 1;
                Console.WriteLine($"{zoekStad} staat in de lijst op positie {positie}.");
            }
            else
            {
                Console.WriteLine($"{zoekStad} staat niet in de lijst.");
            }
        }
    }
}

Drillen 9 — Favorieten beheren

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> favorieten = new List<string>() { "Pizza", "Pasta", "Sushi", "Burger" };

            Console.WriteLine("Jouw favorieten:");
            for (int i = 0; i < favorieten.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {favorieten[i]}");
            }

            string keuze;
            do
            {
                Console.WriteLine("t=toevoegen, v=verwijderen, s=stoppen:");
                keuze = Console.ReadLine().ToLower();

                if (keuze == "t")
                {
                    Console.WriteLine("Wat wil je toevoegen?");
                    string item = Console.ReadLine();
                    favorieten.Add(item);
                    Console.WriteLine("Lijst bijgewerkt:");
                    for (int i = 0; i < favorieten.Count; i++)
                    {
                        Console.WriteLine($"{i + 1}. {favorieten[i]}");
                    }
                }
                else if (keuze == "v")
                {
                    Console.WriteLine("Wat wil je verwijderen?");
                    string item = Console.ReadLine();
                    if (favorieten.Contains(item))
                    {
                        favorieten.Remove(item);
                        Console.WriteLine($"{item} is verwijderd.");
                        Console.WriteLine("Lijst bijgewerkt:");
                        for (int i = 0; i < favorieten.Count; i++)
                        {
                            Console.WriteLine($"{i + 1}. {favorieten[i]}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"{item} staat niet in de lijst.");
                    }
                }
            } while (keuze != "s");
        }
    }
}

Drillen 10 — Muzieklijst

using System;
using System.Collections.Generic;

namespace H8c
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> nummers = new List<string>();

            string invoer;
            do
            {
                Console.WriteLine("Geef een nummer in (of STOP om te stoppen):");
                invoer = Console.ReadLine();
                if (invoer.ToUpper() != "STOP")
                {
                    nummers.Add(invoer);
                }
            } while (invoer.ToUpper() != "STOP");

            string keuze;
            do
            {
                Console.WriteLine("Maak een keuze:");
                Console.WriteLine("a: Toon alle nummers");
                Console.WriteLine("b: Zoek een nummer");
                Console.WriteLine("c: Verwijder een nummer");
                Console.WriteLine("x: Stop");
                keuze = Console.ReadLine().ToLower();

                if (keuze == "a")
                {
                    Console.WriteLine($"Je lijst bevat {nummers.Count} nummer(s):");
                    for (int i = 0; i < nummers.Count; i++)
                    {
                        Console.WriteLine($"{i + 1}. {nummers[i]}");
                    }
                }
                else if (keuze == "b")
                {
                    Console.WriteLine("Welk nummer zoek je?");
                    string zoekNummer = Console.ReadLine();
                    if (nummers.Contains(zoekNummer))
                    {
                        Console.WriteLine($"{zoekNummer} staat in je lijst.");
                    }
                    else
                    {
                        Console.WriteLine($"{zoekNummer} staat niet in je lijst.");
                    }
                }
                else if (keuze == "c")
                {
                    Console.WriteLine("Welk nummer wil je verwijderen?");
                    string verwijderNummer = Console.ReadLine();
                    if (nummers.Contains(verwijderNummer))
                    {
                        nummers.Remove(verwijderNummer);
                        Console.WriteLine($"{verwijderNummer} is verwijderd.");
                    }
                    else
                    {
                        Console.WriteLine($"{verwijderNummer} staat niet in je lijst.");
                    }
                }
                else if (keuze != "x")
                {
                    Console.WriteLine("Ongeldige keuze.");
                }
            } while (keuze != "x");
        }
    }
}