4.2 KiB
Arrays and Lists
Overview
Arrays
Multidimensional Arrays
Lists
Iterating a List
Foreach
Arrays
Arrays are a collection of variables of the same type, which allocate neighboring memory locations
A single value in this collection is called an element
Arrays can be __declared __ with square brackets following the type of the elements:
int[] userIds;
Declaring an array does not yet allocate space from memory
Arrays (continued)
__Initialize __ an array with a length of 3 using the following syntax:
int[] userIds = new int[3];
Assign a value to an element in an array by specifying the index in square brackets:
userIds[0] = 104;
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:
string[] names = new string[3] { "Johannes", "Rene", "Ville" };
The same works without specifying the length in the brackets:
double[] balances = new double[] { 1.3, 200.3, 9332.14 };
Multidimensional Arrays
C# supports multidimensional arrays:
char[,] letters = new char[3, 4]
{
{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'}
};
Console.WriteLine(letters[1, 3]); // Outputs "h"
Lists
The .NET Framework Class Library offers another way to hold multiple variables: the _List _ object
Declaration and initialization:
List userIds = new List();
Value assignment:
userIds[0] = 22;
Lists (continued)
The key benefit of using Lists lies in its built-in functionalities
You can see a list of available methods in VSC2019 by following a variable with a dot
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
Arrays vs Lists (continued)
When to use arrays and when lists?
Use __arrays __ if you need high performance
Use __lists __ if you need support for list operations
Iterating an Array With for Loop - Example
string[] names = new string[]
{
"Harry Potter",
"Luke Skywalker",
"Harley Quinn"
};
for(int i = 0; i < names.Length; ++i)
{
Console.WriteLine(names[i]);
}
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
List numbers = new List() { 1, 5, 3 };
int sum = 0;
for (int i = 0; i < numbers.Count; ++i)
{
sum += numbers[i];
}
// sum is now 9
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:
string[] names = new string[]
{
"Harry Potter",
"Luke Skywalker",
"Harley Quinn"
};
foreach (string name in names)
{
Console.WriteLine(name);
}
foreach Statement (continued)
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
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.
( Note : you can use int.Parse() -method to parse the user input string to an integer)