Topics covered:

  • Declaring and Creating Arrays
  • Accessing Array Elements
  • Console Input and Output of Arrays
  • Iterating Over Arrays Using for and foreach
  • Dynamic Arrays (List<T>)
  • Copying Arrays

Video (in Bulgarian)

Presentation Content

What are Arrays?

  • An array is a sequence of elements
    • All elements are of the same type
    • The order of the elements is fixed
    • Has fixed size ( Array.Length )

Declaring Arrays

  • Declaration defines the type of the elements
  • Square brackets [] mean “array”
  • Examples:
    • Declaring array of integers: int[] myIntArray;
    • Declaring array of strings: string[] myStringArray;

Creating Arrays

  • Use the operator new
    • Specify array length
  • Example creating (allocating) array of 5 integers: myIntArray = new int[5];

Creating and Initializing Arrays

  • Creating and initializing can be done together:
  • The new operator is not required when using curly brackets initialization
    • myIntArray = {1, 2, 3, 4, 5};
1 2 3 4 5

How to Access Array Element?

  • Array elements are accessed using the square brackets operator [] (indexer)
    • Array indexer takes element’s index as parameter
    • The first element has index 0
    • The last element has index Length-1
  • Array elements can be retrieved and changed by the [] operator

Processing Arrays: for Statement

  • Use for loop to process an array when
    • Need to keep track of the index
    • Processing is not strictly sequential from the first to the last element
  • In the loop body use the element at the loop index ( array[index ] ):

Processing Arrays: foreach

  • How foreach loop works?
    • type – the type of the element
    • value – local name of variable
    • array – processing array
  • Used when no indexing is needed
    • All elements are accessed one by one
    • Elements can not be modified (read only)

Lists (Resizable Arrays)

  • List<T> – array that can resize dynamically
    • When adding or removing elements
    • Also have indexers [] (like arrays)
    • T is the type that the list will hold
      • E.g. List<int> will hold integers
      • List<object> will hold objects
  • Basic methods and properties
    • Add(T element) – adds new element to the end
    • Remove(element) - – removes the element
    • Count – returns the current size of the list

How The List<T> Works?

  • Why adding new elements is not slow?
    • When adding n elements in List<T> it resizes itself log (2) _n _ times instead of n
  • Initially a new List<T> has size of 0 elements
    • Counter for total capacity ( Capacity )
    • Counter for number of used capacity ( Count )
    • When created, both properties of the list have values of 0
    • When adding the first element Count becomes 1 and Capacity becomes 4
  • Initially the List<T> is empty
    • When adding new element it is resized
    • But not every time
      • Only when it is needed
  • Lets have a list with 3 elements
    • It looks like this:
    • When we add new elementit is appended to the end
    • Adding a fifth element doubles the Capacity of the list

Copying Arrays

  • Sometimes we must copy the values from one array to another one
    • If we do it the intuitive way we would copy not only the values but the reference to the array
      • Changing some of the values in one array will affect the other
      • int[] copyArray = array;
    • The way to avoid this is using Clone()
      • This way only the values will be copied but not the reference
      • int[] copyArray = (int[])array.Clone();

Summary

  • Arrays are a fixed-length sequences of elements of the same type
  • Array elements are accessible by index
    • Can be read and modified
  • Iteration over array elements can be done with for and foreach loops
  • List<T> holds resizable arrays
    • Good when we don’t know the number of elements initially