Arrays and lists#

Arrays#

Defining and initializing an array#

string[] robotNames = ["HAL 9000", "R2-D2", "WALL-E"];
double[] robotJointDegAngles = [-135.4, 38.5, 90, 0, 0, 23.5];
int[] primeNumbers = [2, 3, 5, 7, 11, 13];

// multi-dimensional array
double[][] caloriesEachDayEachMeal =
[
    [350.0, 620.5, 800.0], // First day (breakfast, lunch, dinner)
    [300.0, 590.0, 620.3, 200.6], // Second day (+ night snack)
    [940.2, 790.5] // etc
];
Array (data structure)

A data structure consisting of a collection of elements (values or variables), of same memory size, each identified by at least one array index or key.

collection expression ([...])

a C# expression which is used to create collection values for various types

Activity 18

Which of the following is/are arrays?

  1. int array = 5

  2. string[] choices = ["black bird", "great tit", "falcon"]

  3. int[] part_ids= [3093, -49318, 3092.812];

  4. string[] names = []

Useful operations#

int[] numbers = [5, 2, 9, 1];
var hasFour = numbers.Contains(4); // false
var thirdItem = numbers[2]; // 9
var total = numbers.Length; // 4
numbers[2] = 5; // modification to [5, 2, 5, 1]
Console.WriteLine(string.Join(" ", numbers)); // 5, 2, 5, 1

double[][] energyEachDayEach8Hour =
[
    [5.6, 10.4, 30.5],
    [4.2, 9.4]
];
var dayTwoNight = energyEachDayEach8Hour[1][0];

Tip

Use breakpoints to visualize the values of individual operations. You can set a breakpoint in the first lines and then click beside a line to run the code until this line. The feature is called Run to cursor.

You can see the state of the variables:

  • in the end of the line where they are defined (gray color), or

  • in Threads & Variables on the debugging window.

Arrays are the stepping stone to lists#

After an array is created in C#, its size is fixed – this can be a limitation. Now we will introduce List<..>, which is a more advanced collection. Still, it is helpful to know how arrays work, because arrays exist in most programming languages.

Lists#

List is an array that resizes automatically. They can be used the same way as arrays, but provide more functionality.

So, use lists instead of arrays.

Defining and initializing a list#

Using the same template from arrays

Useful operations#

List<int> numbers = [5, 2, 9, 1];
var hasFour = numbers.Contains(4); // false
var total = numbers.Count; // 4
// ️⚠️ Called Count, because Length implies fixed length

var thirdItem = numbers[2]; // 9
numbers[2] = 5; // modification to [5, 2, 5, 1]
Console.WriteLine(string.Join(" ", numbers)); // 5, 2, 5, 1

List<List<double>> energyEachDayEach8Hour =
[
    [5.6, 10.4, 30.5],
    [4.2, 9.4]
];
var dayTwoNight = energyEachDayEach8Hour[1][0]; // 4.2

Additional operations over arrays#

List<int> numbers = [5, 2, 9, 1];
numbers.Add(5); // [5, 2, 9, 1, 5]
numbers.Insert(1, 3); // [5, 3, 2, 9, 1, 5]
numbers.Remove(5); // [3, 2, 9, 1, 5]
numbers.RemoveAt(2); // [3, 2, 1, 5] 
var id = numbers.IndexOf(1); // 2
var sum = numbers.Sum(); // 11

Activity 19

Copy the code above in your editor. Then, find three different operations you can do on lists using two ways:

  1. Use autocompletion, i.e., write numbers. and pick one interesting method and come with an example

  2. Use an LLM to come up with other two examples.

Steps:

  1. 5 min individually

  2. 2 min share with your partner

  3. 2 min one student shares to the class

Appendix#