# Arrays and lists

## Arrays

(defining-and-initializing-an-array-examples)=
### Defining and initializing an array

```cs
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
];
```

:::{wpd} 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}
:label: array-or-not
Which of the following is/are arrays?

1. `int array = 5`{l=cs}
1. `string[] choices = ["black bird", "great tit", "falcon"]`{l=cs}
1. `int[] part_ids= [3093, -49318, 3092.812];`{l=cs}
1. `string[] names = []`{l=cs}
:::

### Useful operations

```cs
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]

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

### 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.

(list)=
## List

`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](project:#defining-and-initializing-an-array-examples)

### Useful operations

```cs
List<int> numbers = [5, 2, 9, 1];
var hasFour = numbers.Contains(4); // false
var total = numbers.Count; // 4

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

```cs
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
```
:::{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.
:::

:::{activity}
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
1. Use an LLM to come up with other two examples.

Steps:
1. 3 min individually
1. 2 min share with your partner
1. 2 min one student shares to the class
:::

## Appendix

- [Arrays](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays)
- [Collection expressions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions)