Course: CSCI 1260

Arrays

  • Homogenous, fixed-length data structure
  • Size is determined on initialization
  • Since Array is a fixed-length data structure, you need to copy them to add data.
    • Although you’re better off using a List at this point
static T[] AddToArray<T>(T[] array, T item)
{
    var newArr = new T[array.Length + 1];
    for (var i = 0; i < array.Length; i++) newArr[i] = array[i];
 
    newArr[^1] = item;
    return newArr;
}

Multi-Dimensional Arrays

  • Arrays that have more than one dimension, such as a grid
  • All rows must have the same length
var map = new int[3, 3, 3]; // 3x3x3 β€” 3d array

Jagged Arrays

  • Arrays of arrays where the rows can be different lengths
  • More memory efficient in certain scenarios over multi-dimensional arrays
var jaggedArr = new int[3][][]; // 3 rows

Stack

  • LIFO β†’ last in, first out
    • The most recently added item is the one that gets removed when you remove an item
  • Stack<T>.Push β†’ adds item to the top of the stack
  • Stack<T>.Pop β†’ removes items from the top of the stack
var cardStack = new Stack<Card>();  
cardStack.Push(new Card("Queen", "Hearts"));  
cardStack.Push(new Card("Ace", "Diamonds"));  
cardStack.Push(new Card("Six", "Spades"));  
cardStack.Pop(); // Six of Spades

Queue

  • FIFO β†’ first in, first out
    • Items are removed sequentially, in the order that they arrived
  • Queue<T>.Enqueue β†’ adds item to the front of the queue
  • Queue<T>.Dequeue β†’ removes items from the back of the queue
var cardQueue = new Queue<Card>();  
cardQueue.Enqueue(new Card("King", "Clubs"));  
cardQueue.Enqueue(new Card("Ten", "Hearts"));  
cardQueue.Enqueue(new Card("Two", "Diamonds"));  
cardQueue.Dequeue(); // King of Clubs

Dictionary

  • Unordered, key/value pairs
  • Dictionary<T>.Remove β†’ removes an item from the dictionary
  • Dictionary<T>[Item] β†’ access the item with key Item in the dictionary
var areas = new Dictionary<string, string>();  
areas.Add("Square", "s^2");  
areas.Add("Circle", "Ο€r^2");  
areas.Add("Triangle", "1/2bh");  
Console.WriteLine(areas["Circle"]); // Ο€r^2