List Methods
In the previous chapter we learnt how to declare, initialize and add elements to a list. Additionally we also looked at how we would access elements of a list and loop through them. In this chapter we will look at some useful list methods.
1. Remove()
The Remove
method removes the first instance of an element from a list.
Syntax: 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 + " "); } } } }
In case no such element is found, it simply doesn't do anything:
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 + " "); } } } }
2. RemoteAt()
The RemoveAt
method removes an element at a specific index.
Syntax: 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 + " "); } } } }
3. Clear
The Clear
method simply removes all elements from a list. It takes no arguments.
Syntax: 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 + " "); } } } }
4. Insert
The Insert
method inserts an element in the list at a specified index.
Syntax: 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 + " "); } } }
We can omit the curly braces {}
in case there's only a single statement inside an if-condition, a for-loop, or a foreach loop. For-example:
index.cs
123foreach(int number in numbers) { Console.Write(number + " "); }
The above snippet is the same as:
index.cs
12foreach(int number in numbers) Console.Write(number + " ");
If there's already an element at the specified index, it is pushed to the right, and so are the remaining elements of the array after it - in case there are any. The following diagram illustrates the process:
5. Contains()
The Contains
method checks if a list contains a specific element. It returns a boolean value (true
or false
).
Syntax: 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')); } } }
6. IndexOf()
The IndexOf
method returns the index of the first occurrence of an element in a list.
Syntax: 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')); } } }
If the element doesn't exist in the list, it simply returns -1
:
The indexOf
method is especially useful when we want to access an element by index but we won't know it's index. For-example in the vowels
, we want to access the element o
by index and change it to capital 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. What does the Remove()
method do?
2. What is the fastest way of checking if a list contains a specific element?
3. Which method is used for removing all elements from a list?
4. What will be the output of the following code?
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.04
List Methods
Свайпніть щоб показати меню
In the previous chapter we learnt how to declare, initialize and add elements to a list. Additionally we also looked at how we would access elements of a list and loop through them. In this chapter we will look at some useful list methods.
1. Remove()
The Remove
method removes the first instance of an element from a list.
Syntax: 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 + " "); } } } }
In case no such element is found, it simply doesn't do anything:
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 + " "); } } } }
2. RemoteAt()
The RemoveAt
method removes an element at a specific index.
Syntax: 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 + " "); } } } }
3. Clear
The Clear
method simply removes all elements from a list. It takes no arguments.
Syntax: 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 + " "); } } } }
4. Insert
The Insert
method inserts an element in the list at a specified index.
Syntax: 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 + " "); } } }
We can omit the curly braces {}
in case there's only a single statement inside an if-condition, a for-loop, or a foreach loop. For-example:
index.cs
123foreach(int number in numbers) { Console.Write(number + " "); }
The above snippet is the same as:
index.cs
12foreach(int number in numbers) Console.Write(number + " ");
If there's already an element at the specified index, it is pushed to the right, and so are the remaining elements of the array after it - in case there are any. The following diagram illustrates the process:
5. Contains()
The Contains
method checks if a list contains a specific element. It returns a boolean value (true
or false
).
Syntax: 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')); } } }
6. IndexOf()
The IndexOf
method returns the index of the first occurrence of an element in a list.
Syntax: 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')); } } }
If the element doesn't exist in the list, it simply returns -1
:
The indexOf
method is especially useful when we want to access an element by index but we won't know it's index. For-example in the vowels
, we want to access the element o
by index and change it to capital 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. What does the Remove()
method do?
2. What is the fastest way of checking if a list contains a specific element?
3. Which method is used for removing all elements from a list?
4. What will be the output of the following code?
Дякуємо за ваш відгук!