finish lecture 5
parent
36809c09b3
commit
2f29b5f8ea
File diff suppressed because one or more lines are too long
@ -1,204 +1,208 @@
|
|||||||
# Arrays and Lists
|
---
|
||||||
|
marp: true
|
||||||

|
paginate: true
|
||||||
|
math: mathjax
|
||||||
|
theme: buutti
|
||||||
|
title: 5. Arrays and Lists
|
||||||
---
|
---
|
||||||
|
|
||||||
# Overview
|
# Arrays and Lists
|
||||||
|
|
||||||
Arrays
|
|
||||||
|
|
||||||
Multidimensional Arrays
|
|
||||||
|
|
||||||
Lists
|
|
||||||
|
|
||||||
Iterating a List
|
|
||||||
|
|
||||||
Foreach
|
|
||||||
|
|
||||||
# Arrays
|
<!-- headingDivider: 5 -->
|
||||||
|
<!-- class: invert -->
|
||||||
|
|
||||||
Arrays are a collection of variables of the same type, which allocate neighboring memory locations
|
## Overview
|
||||||
|
|
||||||
A single value in this collection is called an __element__
|
* Arrays
|
||||||
|
* Multidimensional arrays
|
||||||
|
* Lists
|
||||||
|
* Iterating a list
|
||||||
|
* Foreach
|
||||||
|
|
||||||
Arrays can be __declared __ with square brackets following the type of the elements:
|
## Arrays
|
||||||
|
|
||||||
|
* Arrays are a collection of variables of the same type
|
||||||
|
* To optimize memory usage, arrays allocate neighbouring memory addresses
|
||||||
|
* A single value in this collection is called an *__element__*
|
||||||
|
* Arrays can be *__declared__* with square brackets following the type of the elements:
|
||||||
|
```csharp
|
||||||
int[] userIds;
|
int[] userIds;
|
||||||
|
```
|
||||||
|
* Declaring an array does not yet allocate space from memory
|
||||||
|
|
||||||
Declaring an array does not yet allocate space from memory
|
### Initializing arrays
|
||||||
|
|
||||||
# Arrays (continued)
|
|
||||||
|
|
||||||
__Initialize __ an array with a length of 3 using the following syntax:
|
|
||||||
|
|
||||||
|
* *__Initialize__* an array with a length of 3 using the following syntax:
|
||||||
|
```csharp
|
||||||
int[] userIds = new int[3];
|
int[] userIds = new int[3];
|
||||||
|
```
|
||||||
__Assign a value__ to an element in an array by specifying the index in square brackets:
|
* *__Assign a value__* to an element in an array by specifying the index in square brackets:
|
||||||
|
```csharp
|
||||||
userIds[0] = 104;
|
userIds[0] = 104;
|
||||||
|
```
|
||||||
|
* Indexing starts from 0, so the above line assigns a value of `104` to the ***first*** element of the array
|
||||||
|
|
||||||
Indexing starts from 0, so the above line assigns a value of 104 to the first element of the array
|
---
|
||||||
|
|
||||||
You can also create an array containing values with one statement:
|
|
||||||
|
|
||||||
|
* You can also create an array and instantly populate it with values with one statement:
|
||||||
|
```csharp
|
||||||
string[] names = new string[3] { "Johannes", "Rene", "Ville" };
|
string[] names = new string[3] { "Johannes", "Rene", "Ville" };
|
||||||
|
```
|
||||||
The same works without specifying the length in the brackets:
|
* The same works without specifying the length in the brackets:
|
||||||
|
```csharp
|
||||||
double[] balances = new double[] { 1.3, 200.3, 9332.14 };
|
double[] balances = new double[] { 1.3, 200.3, 9332.14 };
|
||||||
|
```
|
||||||
|
|
||||||
# Multidimensional Arrays
|
## Multidimensional Arrays
|
||||||
|
|
||||||
C# supports multidimensional arrays:
|
|
||||||
|
|
||||||
|
* C# supports multidimensional arrays
|
||||||
|
* Here's an example of a two-dimensional array.
|
||||||
|
* You can think of it as an array consisting of arrays
|
||||||
|
```csharp
|
||||||
char[,] letters = new char[3, 4]
|
char[,] letters = new char[3, 4]
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
{'a', 'b', 'c', 'd'},
|
{'a', 'b', 'c', 'd'},
|
||||||
|
|
||||||
{'e', 'f', 'g', 'h'},
|
{'e', 'f', 'g', 'h'},
|
||||||
|
|
||||||
{'i', 'j', 'k', 'l'}
|
{'i', 'j', 'k', 'l'}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Console.WriteLine(letters[1, 3]); // Outputs "h"
|
Console.WriteLine(letters[1, 3]); // Outputs "h"
|
||||||
|
```
|
||||||
|
|
||||||
# Lists
|
## Lists
|
||||||
|
|
||||||
The .NET Framework Class Library offers another way to hold multiple variables: the _List _ object
|
|
||||||
|
|
||||||
Declaration and initialization:
|
|
||||||
|
|
||||||
|
* The .NET Framework Library [`System.Collections.Generic`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic?view=net-9.0) offers another structure to contain multiple variables: the [List](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-9.0) class
|
||||||
|
* Importing the library:
|
||||||
|
```csharp
|
||||||
|
using System.Collections.Generic;
|
||||||
|
```
|
||||||
|
* Declaration and initialization:
|
||||||
|
```csharp
|
||||||
List<int> userIds = new List<int>();
|
List<int> userIds = new List<int>();
|
||||||
|
```
|
||||||
Value assignment:
|
* Value assignment (exactly like arrays):
|
||||||
|
```csharp
|
||||||
userIds[0] = 22;
|
userIds[0] = 22;
|
||||||
|
```
|
||||||
|
|
||||||
# Lists (continued)
|
### Why lists?
|
||||||
|
|
||||||
The key benefit of using Lists lies in its built-in functionalities
|
|
||||||
|
|
||||||
|
* The key benefit of using Lists lies in its built-in functionalities
|
||||||

|

|
||||||
|
* You can see a list (heh) of available methods in Visual Studio by following the list variable with a dot
|
||||||
|
|
||||||
You can see a list of available methods in VSC2019 by following a variable with a dot
|
## Arrays vs Lists
|
||||||
|
|
||||||
# Arrays vs Lists
|
|
||||||
|
|
||||||
The memory allocation of an array is static/fixed, but list memory is dynamic
|
|
||||||
|
|
||||||
This allows the flexible mutation of lists, meaning you can always add or remove and hence change change the length of the list without issues
|
|
||||||
|
|
||||||

|
* The memory allocation of an array is ***static*** a.k.a. fixed, but list memory is ***dynamic***
|
||||||
|
* This allows the flexible mutation of lists, meaning you can always add or remove elements, thus changing the length of the list without issues
|
||||||
|
```csharp
|
||||||
|
List<int> intList = new List<int>() { 1, 9, 4 };
|
||||||
|
intList.Remove(0); // List is now [9, 4]
|
||||||
|
|
||||||
# Arrays vs Lists (continued)
|
int[] intArray = new int[] { 1, 9, 4 };
|
||||||
|
intArray.Remove(0);
|
||||||
|
// Can't do that, array is static
|
||||||
|
// and doesn't include such method
|
||||||
|
```
|
||||||
|
|
||||||
When to use arrays and when lists?
|
### When to use arrays or lists?
|
||||||
|
|
||||||
Use __arrays __ if you need __high performance__
|
* Use arrays when you...
|
||||||
|
* ...need high performance
|
||||||
|
* ...can predetermine the number of elements
|
||||||
|
* Use lists when you...
|
||||||
|
* ...need to add/remove elements dynamically
|
||||||
|
* ...need the list operations
|
||||||
|
|
||||||
Use __lists __ if you need support for __list operations__
|
## Example: Iterating an array with a for loop
|
||||||
|
|
||||||
# Iterating an Array With for Loop - Example
|
<div class='columns21' markdown='1'>
|
||||||
|
<div markdown='1'>
|
||||||
|
|
||||||
|
```csharp
|
||||||
string[] names = new string[]
|
string[] names = new string[]
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
"Harry Potter",
|
"Harry Potter",
|
||||||
|
|
||||||
"Luke Skywalker",
|
"Luke Skywalker",
|
||||||
|
|
||||||
"Harley Quinn"
|
"Harley Quinn"
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < names.Length; ++i)
|
for (int i = 0; i < names.Length; ++i)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
Console.WriteLine(names[i]);
|
Console.WriteLine(names[i]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||

|
</div>
|
||||||
|
<div markdown='1'>
|
||||||
|
|
||||||
# Iterating a List
|

|
||||||
|
|
||||||
You can iterate the elements of a list with a for loop the same way as an array
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
Just use List.Count instead of Array.Length
|
## Iterating a List
|
||||||
|
|
||||||
|
* You can iterate the elements of a list with a for loop the same way as an array
|
||||||
|
* Just use `List.Count` instead of `Array.Length`
|
||||||
|
```csharp
|
||||||
List<int> numbers = new List<int>() { 1, 5, 3 };
|
List<int> numbers = new List<int>() { 1, 5, 3 };
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
|
|
||||||
for (int i = 0; i < numbers.Count; ++i)
|
for (int i = 0; i < numbers.Count; ++i)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
sum += numbers[i];
|
sum += numbers[i];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sum is now 9
|
// sum is now 9
|
||||||
|
```
|
||||||
|
|
||||||
# foreach Statement
|
## `foreach` Statement
|
||||||
|
|
||||||
There is one more statement to use to iterate arrays and lists: the foreach statement
|
* There is one more statement to use to iterate arrays and lists: the `foreach` statement
|
||||||
|
* `foreach` is useful when you have to execute code for each element of an array or a list, and don't need the corresponding index:
|
||||||
|
|
||||||
foreach is useful when you have to execute code for each element of an array or a list:
|
<div class='columns21' markdown='1'>
|
||||||
|
<div markdown='1'>
|
||||||
|
|
||||||
|
```csharp
|
||||||
string[] names = new string[]
|
string[] names = new string[]
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
"Harry Potter",
|
"Harry Potter",
|
||||||
|
|
||||||
"Luke Skywalker",
|
"Luke Skywalker",
|
||||||
|
|
||||||
"Harley Quinn"
|
"Harley Quinn"
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (string name in names)
|
foreach (string name in names)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
Console.WriteLine(name);
|
Console.WriteLine(name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div markdown='1'>
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
# foreach Statement (continued)
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
However, foreach creates a copy of each element in the object so the element cannot be mutated directly:
|
### Note about `foreach`
|
||||||
|
|
||||||
|
* However, `foreach` creates a copy of each element in the object so the element cannot be mutated directly:
|
||||||

|

|
||||||
|
* Performance-wise, using foreach is also more costly because it uses more memory space
|
||||||
|
* If you need to change every element, consider creating a new list instead
|
||||||
|
|
||||||
Performance-wise, using foreach is also more costly because it uses more memory space
|
## Exercise 1: Expanding the Console Application
|
||||||
|
<!--_class: "exercise invert" -->
|
||||||
---
|
|
||||||
|
|
||||||
Instead, create a new list
|
|
||||||
|
|
||||||
# Exercise 1: Expanding the Console Application
|
|
||||||
|
|
||||||
Continue working on the command line application you created in "The Main Loop" exercise. Add a new command "add" which prompts the user to write a note.
|
|
||||||
|
|
||||||
After the user has inputted the note, it is saved to a list, and the program returns back to listening to commands.
|
|
||||||
|
|
||||||
Add another command "list" which prints all the saved notes.
|
|
||||||
|
|
||||||
Add one more command "remove" which prints all the saved notes with the index of the note, and then prompts the user for a number. After entering the number the corresponding note is deleted from the list.
|
1) Continue working on the command line application you created in [Lecture 4, Exercise 1: The Main Loop ](4-loops#exercise-1-the-main-loop). Add a new command `add` which prompts the user to write a note.
|
||||||
|
2) After the user has inputted the note, save it to a list, and return back to listening to commands.
|
||||||
|
3) Add another command `list` which prints all the saved notes.
|
||||||
|
4) Add one more command `remove` which prints all the saved notes with the index of the note, and then prompts the user for a number. After entering the number the note with the corresponding index is deleted from the list.
|
||||||
|
|
||||||
( __Note__ : you can use int.Parse() -method to parse the user input string to an integer)
|
* *__Note__*: you can use the `int.Parse()` method to parse the user input string to an integer
|
||||||
|
|
||||||
# Assignments
|
## Assignments
|
||||||
|
|
||||||
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&%20Lists)
|
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&%20Lists)
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 30 KiB |
Loading…
Reference in New Issue