Week 2 Flashcards
Data Structures - Arrays & Lists (19 cards)
What are arrays?
Arrays are collections of elements stored in contiguous memory locations. Zero-based indexed: first element is accessed with index 0.
Characteristics of fixed-size arrays?
- Fixed size after initialisation.
- All elements must be of the same type.
- Direct memory access allows fast read/write operations.
When would you use fixed-size arrays?
When the number of elements is predetermined
What are 2D arrays represented as?
A grid or table.
What are Jagged arrays?
Arrays of arrays where each sub-array can have a different size.
Jagged arrays vs 2D arrays
- 2D arrays use contiguous memory, optimising access speed.
- Jagged arrays require additional memory for references but are more flexible.
How do you access elements of arrays?
Using indices: Console.WriteLine(numbers[2]);
How do you search and sort arrays?
Using in-built methods like Array.Sort() or implemening custom algorithms.
int[] data = { 3, 1, 4 };
Array.Sort(data);
How would you adjust the size of an array?
Array.Resize(ref array, newSize)
How would you find the index of a value of an array?
Array.IndexOf(array, value)
How can you copy elements from one array to another?
Array.Copy(source, destination, length)
What is a list?
A dynamic array that resizes automatically as elements are
added or removed.
List<int> numbers = new List<int>();</int></int>
Key features of lists?
- Resizable collections without manually reallocating memory.
- Provides higher-level abstraction than arrays.
What is Internal Array Resizing
When capacity is exceeded, the internal array size doubles, ensuring
efficiency while minimising reallocation overhead.
Use cases of lists?
For collections where the size can change frequently.
What is a Linked List?
A node-based structure where each node contains data
and references to the next (and optionally previous) node.
LinkedList<int> list = new LinkedList<int>();</int></int>
Characteristics of Linked Lists?
- Memory efficient, especially for dynamic insertions and deletions.
- Sequential access is slower compared to arrays
How are linked list insert/delete operations?
Efficient insertion and deletion since only node references are updated.
Use case of Linked Lists?
For scenarios requiring frequent insertions or deletions at
arbitrary positions.