Programming in Java - Data Structures - Array ArrayList LinkedList
Programming in Java - Data Structures - Array ArrayList LinkedList
byte[] nums = {23, 42, -12, 96, 54, 39, 54, 33};
new value (say, 124) to the “next available” element would id[“nums”] → byte[8]@A2
look like this in code: 1
id[“nums2”] → byte[16]@64 2B
// Extra element we want to append to the array: 2
Reference to new array, nums2
byte newNum = 124; 3
// Create new array that can fit the extra element:
byte[] nums2 = new byte[16]; 4
// Loop to copy all the values 5
// from the old array into the new one:
for (int i=0; i < nums.length; i = i+1) { 6 23 42
0 42
23 -12
0 -12 96
0 96
0 54 0 54
0 39 0 33 0 0
0 124 0 0
nums2[i] = nums[i]; 7 0 0 0 0
}
// Copy the extra value into the next "available" element: 8
nums2[nums.length] = newNum; 9
// If we want to print out the new array: A 23 42
23 42 -12 96 54 39 54 33
-12 96
System.out.println(Arrays.toString(nums2)); B
The console output is: C
[23, 42, -12, 96, 54, 39, 54, 33, 124, 0, 0, 0, 0, 0, 0, 0]
D
(Numerical arrays are created with 0s as default values.)
E
FF
The figure shows what happens in memory F
Arrays 0 1 2 3 4 5 6
MEMORY
7 8 9 A B C D E F
Once we’ve created the larger array with extra room, it can
fit more values. Suppose we now wish to add an extra value 0 00
Node
Linked lists HEXADECIMAL form MEMORY
BINARY form 0 1 2 3 4 5 6 7 8 9 A B C D E F
The reference Java associates with a
00
linked list identifier, llNums, includes: B:0000 = 0 id[“llNums”] → <byte>@A2
• The type of values it holds (in this B:0001 = 1 The reference is kept in the stack. The
case, byte). most important part is the address (A2). 2B
B:0010 = 2
• The memory address (in the heap) of B:0011 = 3
its first element (A2). This element is
the value of the first node of the list, B:0100 = 4 54 @CD
called the head. B:0101 = 5
Now each node has two parts: B:0110 = 6 -12 @87
• A value (of the specified type for the B:0111 = 7 42 @66
list; in this case, byte). B:1000 = 8 96 @A6
• A reference (memory address) to the B:1001 = 9
next node in the linked list. (For the
B:1010 = A 23 @73 54 @D8
last node (the tail) the reference is a
special value called NULL (usually B:1011 = B
implemented as a non-accessible B:1100 = C 33 NULL
memory position, such as 00.)
B:1101 = D 39 @4C
The figure shows a linked list reference in B:1110 = E
the stack, and its component nodes in FF
B:1111 = F
the heap (arrows are only a visual aid).
Adding elements to linked lists
New elements can be inserted into a linked list by simply:
• Step 0: Creating a new node that contains the new value (the new
object) we want on the list.
• Step 1: Making the new node “point to” a node already in the list.
This will be the new node’s next node in the list.
• Step 2: Changing the reference of the node that was already pointing
to the new node’s next node, so that it now points to the new node.
Array List
Size can
Fixed size
change
One or
more Only linear
dimensions
In Java, a LinkedList can only have objects as its elements. In order to include
primitive type values (int, double, char, …) we need to “wrap” them in an object.
Java provides “wrapper classes” for primitive types. These are what we need for
linked lists.
For instance, if we want a linked list for integer objects, we declare that the
elements in the linked list are going to be members (instances) of the class Integer.
Java Wrapper Classes
Primitive data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
https://www.w3schools.com/java/java_wrapper_classes.asp
Adding items to a linked list
Output
Removing items from a
linked list
Output
Be careful when getting the size!
.size() → for Linked Lists
e.g. int k = zones.size(); // zones is a linked list
void add(E e) Adds element e (an object of type E) at the end of the linked list.
Inserts element e (an object of type E) at the beginning of the linked list. (The node containing the new
void addFirst(E e)
element becomes the new head of the linked list, and it points to (its next node is) the previous head.)
Inserts the specified element, e, at the specified index position in the list. Shifts the element currently at
void add(int index, E e) that position (if any), and any subsequent elements, to the right (i.e., adds one to their “indices”).
Throws IndexOutOfBoundsException if the index is out of range (index < 0 or index > size()).
E removeLast() Removes and returns the last element from the list.
E remove(int index) Removes and returns the element at the specified index position in the list.
Throws IndexOutOfBoundsException if the index is out of range (index < 0 or index >= size()).
Removes the first occurrence of the specified element, e, from this list, if it is present.
boolean remove(E e)
Returns true if the list contained the specified element (false otherwise).
E get(int index) Returns the element at the specified index position. The element is not removed from the list.
Throws IndexOutOfBoundsException if the index is out of range (index < 0 or index >= size()).
E set(int index, E e) Replaces the element at the specified position in the list with the specified element, e.
Throws IndexOutOfBoundsException if the index is out of range (index < 0 or index >= size()).
boolean contains(E e) Returns true if this list contains the specified element.
Returns the index of the first occurrence of the specified element, e, in the list.
int indexOf(E e)
Returns -1 if the list does not contain the element.
void clear() Removes all the elements from this list. The list will be empty after this call returns.
HL-only
content
follows!
Binary search tree traversals
Java
Collections
Framework