Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Listamenetelmät | Tietorakenteet ja Tiedostojen Käsittely
C# Perusteiden Jälkeen

bookListamenetelmät

Tässä luvussa tarkastellaan joitakin hyödyllisiä listametodeja.

Remove()

Remove-metodi poistaa ensimmäisen esiintymän tietystä alkiosta listasta.

Syntaksi:

exampleList.remove(targetElement);
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Banana", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Remove("Banana"); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Jos tällaista alkiota ei löydy, metodi ei tee mitään.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Banana", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Remove("some element"); // trying to remove an unknown element Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

RemoveAt()

RemoveAt-metodi poistaa alkion tietyssä indeksissä.

Syntaksi:

exampleList.RemoveAt(index);
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.RemoveAt(1); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Clear

Clear-metodi poistaa kaikki alkiot listasta. Se ei ota argumentteja.

Syntaksi:

exampleList.Clear();
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Clear(); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Insert

Insert-metodi lisää alkion listaan määritettyyn indeksiin.

Syntaksi:

exampleList.Insert(index, elementToInsert);
index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 2, 4, 6, 10, 12 }; Console.Write("Before: "); foreach (int number in numbers) Console.Write(number + " "); numbers.Insert(3, 8); Console.Write("\nAfter: "); foreach (int number in numbers) Console.Write(number + " "); } } }

Voimme jättää aaltosulkeet {} pois, jos if-ehdossa, for-silmukassa tai foreach-silmukassa on vain yksi lause.

index.cs

index.cs

copy
123
foreach(int number in numbers) { Console.Write(number + " "); }

Yllä oleva koodinpätkä vastaa seuraavaa:

index.cs

index.cs

copy
12
foreach(int number in numbers) Console.Write(number + " ");

Jos määritetyssä indeksissä on jo alkio, se siirtyy oikealle, samoin kaikki sen jälkeiset taulukon alkiot – mikäli niitä on. Seuraava kaavio havainnollistaa prosessia:

Contains()

Contains-metodi tarkistaa, sisältääkö lista tietyn alkion. Se palauttaa totuusarvon (true tai false).

Syntaksi: exampleList.Contains(element);

index.cs

index.cs

copy
1234567891011121314
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; Console.WriteLine("Contains 'o': " + vowels.Contains('o')); } } }

IndexOf()

IndexOf-metodi palauttaa ensimmäisen esiintymän indeksin listassa.

Syntaksi: exampleList.IndexOf(element);

index.cs

index.cs

copy
123456789101112131415
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; vowels.Remove('o'); Console.WriteLine("Index of 'o': " + vowels.IndexOf('o')); } } }

Jos alkiota ei löydy listasta, metodi palauttaa arvon -1:

indexOf-menetelmä on erityisen hyödyllinen, kun haluat käyttää elementtiä indeksin perusteella, mutta et tiedä sen indeksiä. Esimerkiksi vowels-listassa haluat hakea elementin o indeksin perusteella ja muuttaa sen isoksi kirjaimeksi O:

index.cs

index.cs

copy
12345678910111213141516171819
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; int targetIndex = vowels.IndexOf('o'); Console.WriteLine(vowels[targetIndex]); vowels[targetIndex] = 'O'; Console.WriteLine(vowels[targetIndex]); } } }

1. Mitä Remove()-menetelmä tekee?

2. Mikä on nopein tapa tarkistaa, sisältääkö lista tietyn alkion?

3. Mitä metodia käytetään poistamaan kaikki alkiot listasta?

4. Mikä on seuraavan koodin tuloste?

question mark

Mitä Remove()-menetelmä tekee?

Select the correct answer

question mark

Mikä on nopein tapa tarkistaa, sisältääkö lista tietyn alkion?

Select the correct answer

question mark

Mitä metodia käytetään poistamaan kaikki alkiot listasta?

Select the correct answer

question mark

Mikä on seuraavan koodin tuloste?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you give examples of how to use these list methods?

What happens if I try to remove an element that isn't in the list?

Can you explain the difference between Remove and RemoveAt?

Awesome!

Completion rate improved to 2.04

bookListamenetelmät

Pyyhkäise näyttääksesi valikon

Tässä luvussa tarkastellaan joitakin hyödyllisiä listametodeja.

Remove()

Remove-metodi poistaa ensimmäisen esiintymän tietystä alkiosta listasta.

Syntaksi:

exampleList.remove(targetElement);
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Banana", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Remove("Banana"); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Jos tällaista alkiota ei löydy, metodi ei tee mitään.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Banana", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Remove("some element"); // trying to remove an unknown element Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

RemoveAt()

RemoveAt-metodi poistaa alkion tietyssä indeksissä.

Syntaksi:

exampleList.RemoveAt(index);
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.RemoveAt(1); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Clear

Clear-metodi poistaa kaikki alkiot listasta. Se ei ota argumentteja.

Syntaksi:

exampleList.Clear();
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<string> fruits = new List<string> { "Apple", "Orange", "Banana", "Grape" }; Console.Write("Before: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } fruits.Clear(); Console.Write("\nAfter: "); foreach (string fruit in fruits) { Console.Write(fruit + " "); } } } }

Insert

Insert-metodi lisää alkion listaan määritettyyn indeksiin.

Syntaksi:

exampleList.Insert(index, elementToInsert);
index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 2, 4, 6, 10, 12 }; Console.Write("Before: "); foreach (int number in numbers) Console.Write(number + " "); numbers.Insert(3, 8); Console.Write("\nAfter: "); foreach (int number in numbers) Console.Write(number + " "); } } }

Voimme jättää aaltosulkeet {} pois, jos if-ehdossa, for-silmukassa tai foreach-silmukassa on vain yksi lause.

index.cs

index.cs

copy
123
foreach(int number in numbers) { Console.Write(number + " "); }

Yllä oleva koodinpätkä vastaa seuraavaa:

index.cs

index.cs

copy
12
foreach(int number in numbers) Console.Write(number + " ");

Jos määritetyssä indeksissä on jo alkio, se siirtyy oikealle, samoin kaikki sen jälkeiset taulukon alkiot – mikäli niitä on. Seuraava kaavio havainnollistaa prosessia:

Contains()

Contains-metodi tarkistaa, sisältääkö lista tietyn alkion. Se palauttaa totuusarvon (true tai false).

Syntaksi: exampleList.Contains(element);

index.cs

index.cs

copy
1234567891011121314
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; Console.WriteLine("Contains 'o': " + vowels.Contains('o')); } } }

IndexOf()

IndexOf-metodi palauttaa ensimmäisen esiintymän indeksin listassa.

Syntaksi: exampleList.IndexOf(element);

index.cs

index.cs

copy
123456789101112131415
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; vowels.Remove('o'); Console.WriteLine("Index of 'o': " + vowels.IndexOf('o')); } } }

Jos alkiota ei löydy listasta, metodi palauttaa arvon -1:

indexOf-menetelmä on erityisen hyödyllinen, kun haluat käyttää elementtiä indeksin perusteella, mutta et tiedä sen indeksiä. Esimerkiksi vowels-listassa haluat hakea elementin o indeksin perusteella ja muuttaa sen isoksi kirjaimeksi O:

index.cs

index.cs

copy
12345678910111213141516171819
using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' }; int targetIndex = vowels.IndexOf('o'); Console.WriteLine(vowels[targetIndex]); vowels[targetIndex] = 'O'; Console.WriteLine(vowels[targetIndex]); } } }

1. Mitä Remove()-menetelmä tekee?

2. Mikä on nopein tapa tarkistaa, sisältääkö lista tietyn alkion?

3. Mitä metodia käytetään poistamaan kaikki alkiot listasta?

4. Mikä on seuraavan koodin tuloste?

question mark

Mitä Remove()-menetelmä tekee?

Select the correct answer

question mark

Mikä on nopein tapa tarkistaa, sisältääkö lista tietyn alkion?

Select the correct answer

question mark

Mitä metodia käytetään poistamaan kaikki alkiot listasta?

Select the correct answer

question mark

Mikä on seuraavan koodin tuloste?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
some-alt