Lists

Lists are a general form of ArrayLists. A list contains objects of the same type. There are some methods for adding, inserting, removing ,etc. You can also access the index of an element in the list. Before creating a list, i'd like to start by showing you the differences between lists and arrays.
Why should we use an array instead of a list?
Why should we use a list instead of an array?

The size of an array is fixed once it is allocated otherwise the size of a list can be expanded. List is a generic collection( System.Collections.Generic) but an array is a collection of elements of the same type. Probably you are wondering that which one is more preferable to use. In this case, it is important to define your needs and your tasks which will lead you to prefer one structure.

Creating a list

In the code below, i created three lists, two of them contain integers and the other one contains strings.

List<int> myFirstList = new List<int>();
List<string> cities = new List<string>()
{
    "istanbul","London", "Paris", "LA"
};
List<int> secondList = new List<int>() { 500, 2000, 3000 };

Adding an element into a list

myFirstList.Add(1990);
myFirstList.Add(2020);
myFirstList.Add(1920);
myFirstList.Add(1922);
myFirstList.Insert(0, 1200);// we add 1200 at index 0.
myFirstList.InsertRange(1, secondList);// we Insert elements of secondList at the specified index. 

Accessing an element

string city = cities[1];
Console.WriteLine(city);


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


// For each
foreach (var num in secondList)
{
    Console.WriteLine(num);
}

Clear all the elements from a list and remove an element

myFirstList.Clear()
myFirstList.Remove(123);// removes the first 123.
myFirstList.RemoveAt(1);// remove the element at the index 1.

Checking the existence of an element in a list

We use Contains() to check whether the specified element exists or not in a List.

bool check = myFirstList.Contains(123);// if 123 is in the list, it returns true.
// if 123 is not in the list, we insert it into the list at the index 2.    
if (numCheck == false)
{
    myFirstList.Insert(2, 123);
}

We can get an index of a specified element using BinarySearch()

Console.WriteLine(cities.BinarySearch("London"));

Exercise 1

To clear the code, i add all steps above:

  • Create two lists.
  • Generate 5 numbers randomly for each list.
  • Find the different elements between list1 and list2 and add them into third list called diff.
  • Print items from diff.
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
List<int> diff = new List<int>();
Random rdm1;
Random rdm2;
for (int i = 0; i < 5; i++)
{
             
    rdm1 = new Random();
    rdm2 = new Random();
    list1.Add( rdm1.Next(1, 10));
    list2.Add(rdm2.Next(1, 10));
                
}
            
foreach (var item in list1)
{
    if (!list2.Contains(item)) // we check whatever an element of list1 exists in list2 
    {
        diff.Add(item);
    }
}


foreach (var item in list2)
{
    if (!list1.Contains(item))
    {
        diff.Add(item);
    }
}


foreach (var item in diff)
{
    Console.WriteLine(item);
}

Exercise 2

Firstly we generated 15 number using Random Class and push them into randomList. Then we get a guess from the user and see if it's on the list. If it is in the list, write the index number on the console.


List<int> randomList = new List<int>();
Random rdm;

for (int i = 0; i < 15; i++)
{
    rdm = new Random();
    randomList.Add(rdm.Next(1, 100));
}
foreach (var item in randomList)
{
    Console.Write(item + " ");
}

int guess = Convert.ToInt32(Console.ReadLine());
if (randomList.Contains(guess))
{
    Console.WriteLine(randomList.IndexOf(guess));
}