static void Main(string[] args) { var numbers = new List <int>() { 1, 2, 3, 4 }; //deklaracja listy z wartościami numbers.Add(1); //dodanie elementu do listy numbers.AddRange(new int[3] { 5, 6, 7 }); //dodanie tablicy do listy foreach (int num in numbers) { Console.WriteLine(num); } Console.WriteLine(); //IndexOf() var pos = numbers.IndexOf(1); //1. wystąpienie wartości Console.WriteLine(pos); var pos2 = numbers.LastIndexOf(1); //ostatnie wystąpienie wartości Console.WriteLine(pos2); Console.WriteLine(); //Count - długość listy Console.WriteLine("Count: " + numbers.Count); //Remove //usunięcie pierwszego wystąpienia danej wartości //numbers.Remove(1); /*foreach (int num in numbers) // NIE WOLNO UŻYWAC FOREACH DO USUWANIA EL. Z LISTY * { * if(num ==1) * numbers.Remove(num); * * }*/ Console.WriteLine(); for (var i = 0; i < numbers.Count; i++) { if (numbers[i] == 1) { numbers.Remove(numbers[i]); } } foreach (int num in numbers) { Console.WriteLine(num); } //Clear - usuwa wszystkie elementy z listy numbers.Clear(); Console.WriteLine("Count: " + numbers.Count); }
static void Main(string[] args) { var numbers = new List <int>() { 1, 2, 3, 4 }; // Add() --> it adds an object to a list numbers.Add(1); // AddRange() --> add another list or array of lists numbers.AddRange(new int[3] { 5, 6, 7 }); foreach (var n in numbers) { Console.WriteLine(n); } /* * IndexOf() * - returns the index of a given object or '-1' if object doesn't exist * - it searches from the beginning of the List * - LastIndexOf searches from the end of the List */ Console.WriteLine("Index of 1: " + numbers.IndexOf(1)); Console.WriteLine("Last Index of 1: " + numbers.LastIndexOf(1)); // Count() --> returns the number of objects in the list Console.WriteLine("Count: " + numbers.Count()); /* * Remove() * - cannot modify a collection from within a 'foreach' group */ for (int i = 0; i < numbers.Count; i++) { if (numbers[i] == 1) { numbers.Remove(numbers[i]); } } foreach (var number in numbers) { Console.WriteLine(number); } // we removed both '1s' from the list // Clear() --> removes all items from the List numbers.Clear(); Console.WriteLine("Count of numbers: " + numbers.Count()); // Exercises var exercises = new Exercises(); //exercises.Number1(); //exercises.Number2(); //exercises.Number3(); //exercises.Number4(); exercises.Number5(); }
static void Main(string[] args) { List <int> listOfNumbers = new List <int>() { 1, 2, 3, 4 }; // Add and AddRange listOfNumbers.Add(5); listOfNumbers.AddRange(new int[] { 6, 7, 8 }); foreach (int number in listOfNumbers) { Console.Write(number + " "); } Console.WriteLine(); // IndexOf and LastIndexOf int n = listOfNumbers.IndexOf(1); Console.WriteLine("Index of 1: " + n); int n2 = listOfNumbers.LastIndexOf(3); Console.WriteLine("Last index of 3: " + n2); // Count Console.WriteLine("Count of elements: " + listOfNumbers.Count); // Remove listOfNumbers.Remove(1); foreach (int num in listOfNumbers) { Console.Write(num + " "); } Console.WriteLine(); listOfNumbers.RemoveAt(0); foreach (int num in listOfNumbers) { Console.Write(num + " "); } Console.WriteLine(); for (int i = 0; i < listOfNumbers.Count; i++) { if (listOfNumbers[i] == 6) { listOfNumbers.RemoveAt(i); } } foreach (int num in listOfNumbers) { Console.Write(num + " "); } Console.WriteLine(); listOfNumbers.Clear(); foreach (int num in listOfNumbers) { Console.Write(num + " "); } Console.WriteLine(listOfNumbers.Count); }
static void Main(string[] args) { // Lists are dynamic, whereas Arrays are static // Lists are a generic type // Example List var numbers = new List <int>() { 1, 2, 3, 4 }; numbers.Add(1); numbers.AddRange(new int[3] { 5, 6, 7 }); foreach (var number in numbers) { Console.WriteLine(number); } // IndexOf() Console.WriteLine("Index of 1: " + numbers.IndexOf(1)); // LastIndexOf() Console.WriteLine("Last Index of 1: " + numbers.LastIndexOf(1)); // Count Console.WriteLine("Count: " + numbers.Count); // Remove // foreach throws error, so use regular for loop //foreach (var number in numbers) //{ // if (number == 1) // numbers.Remove(number); //} for (var i = 0; i < numbers.Count; i++) { if (numbers[i] == 1) { numbers.Remove(numbers[i]); } } foreach (var number in numbers) { Console.WriteLine(number); } // Clear() numbers.Clear(); Console.WriteLine("Count: " + numbers.Count); Console.WriteLine(); // Useful List methods // Add() // AddRange() // Remove() // RemoveAll() // IndexOf() // Contains() // Count }