01 - Arrays Flashcards
What is an Algorithm?
A step-by-step procedure for solving a problem
May be performed by a human or a machine
What is a data structure?
A way of organising information
What is an array? (According to John Lewis)
A sequence of indexed elements
It has a fixed length
What is the time complexity for accessing an index in an array?
Accessed in O(1)
Array elements are accessed by their indices which are unique and fixed
What field stores the size of an array in Java?
length
e.g. arrayPotter.length
How are array elements accessed in Java?
Using integers enclosed in square brackets
e.g. array[i]
Can the lower bound of an array be chosen in java?
No
The lower bound is always 0
Are arrays passed by reference or by value?
By reference
Arrays are objects which means that they are manipulated by reference
Can one array hold primitive values of different types?
No
Arrays of primitive types (int, char), can only hold one type
Can an array hold objects from different classes?
Only partly If an array is defined to hold objects of class C, it can also hold any objects which are subclasses of C
Which of these are invalid?
a: int[] myArray;
b: int[] myArray = int[10];
c: int[] myArray = new int[10];
d: int[] myArray = {1,1,2,3,5,8}
b: int[] myArray = int[10]
Array initialisation much be prefixed with the ‘new’ keyword
How do you test equality between arrays?
Arrays.equals(array1,array2)
When are two arrays considered equal?
When they contain exactly the same elements in the same order
Does Arrays.equals() work on 2D arrays?
No
Arrays.deepEquals() must be used instead