DLLists, Arrays
Although SLList provides a efficient way for us to manipulate the list, it's really slow when we want to add an item to the end of the list.
addLast()
Similar to size()
, we could cache the last pointer; however, remove it will be still really slow because we will iterate to the second last item.
We could add a pointer to each node which points to the previous node, but the problem is that the last
pointer of the SLList
class may points to the sentinel node.
Thus, we could add a second sentinel at the end of the list, and point it with sentBack
.
Alternatively, we may just use a single sentinel and make the list circular.
Generic List
We could improve our IntList to make it available for other types.
We put the desired type inside of angle brackets during declaration, and also use empty angle brackets during instantiation.
Array Overview
Definition and Creation
Array is a special kind of object which consists of a numbered sequence of memory boxes. An array consists of:
A fixed integer
length
.A sequence of N memory boxes where
N=length
, and all boxes hold the same type of value, which are numbered from 0 tolength-1
.
Like classes, arrays are instantiated with new
:
Arraycopy
Two ways to copy arrays:
Item by item using a loop.
Using arraycopy: Source array, start position in source, target array, start position in target, number to copy.
2D Array
We could create a 2-dimensional array with the following codes.
Arrays and Classes
Both arrays and classes can be used to organize a bunch of memory boxes. In both cases, the number of memory boxes is fixed, i.e. the length of an array cannot be changed, just as class fields cannot be added or removed.
The key differences between memory boxes in arrays and classes:
Array boxes are numbered and accessed using [] notation, and class boxes are named and accessed using dot notation.
Array boxes must all be the same type. Class boxes can be different types.
Last updated