Listamenetelmä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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
1234567891011121314151617181920212223using 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
123foreach(int number in numbers) { Console.Write(number + " "); }
Yllä oleva koodinpätkä vastaa seuraavaa:
index.cs
12foreach(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
1234567891011121314using 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
123456789101112131415using 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
12345678910111213141516171819using 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?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
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
Listamenetelmä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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
123456789101112131415161718192021222324252627using 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
1234567891011121314151617181920212223using 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
123foreach(int number in numbers) { Console.Write(number + " "); }
Yllä oleva koodinpätkä vastaa seuraavaa:
index.cs
12foreach(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
1234567891011121314using 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
123456789101112131415using 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
12345678910111213141516171819using 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?
Kiitos palautteestasi!