Array definition
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Initialize a one dimensional array that can hold 10 integers
int[] myArr = new int[10];
Initialize a two dimensional array of integers with 10 rows and 20 columns
int[][] myArr = new int[10][20];
Initialize an array containing the integers 1, 2, and 3
int[] myArr = new int[]{1, 2, 3};
or
int[] myArr = {1, 2, 3};
Initialize a two dimensional array with 3 rows and 3 columns:
1, 2, 3
4, 5, 6
7, 8, 9
int[][]myArr={{1,2,3},{4,5,6},{7,8,9}}
Iterate through an array
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
for (int i : array) {
System.out.print(array[i]);
}
String creation
String s = new String(“hello”);
String s = “hello”;
String size
s.length()
Accessing and iterating through characters in a String, s.
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i));
}
HashMap definition
A HashMap is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.
HashMap
HashMap map = new HashMap<>();
for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + “ “ +
entry.getValue());
}
for (String key : map.keySet()) {
System.out.println(key);
}
for (String value : map.values()) {
System.out.println(value);
}
HashMap Time Complexity
HashSet definition
A HashSet is a collection that uses a Hash table for storage, only allowing unique elements to be added.
HashSet
HashSet set = new HashSet<>();
for(String s : set) {
System.out.println(s);
}
HashSet Time Complexity
ArrayList definition
An ArrayList is a collection of data elements sequentially ordered from 0 to length - 1. This means that we are able to access an element inside an ArrayList by its position (index).
ArrayList
ArrayList list = new ArrayList<>();
List list = new ArrayList<>();
list. remove(0); // Remove index 0
list. clear(); // Remove all elements
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for (String s : list) {
System.out.println(s);
}
Collections.sort(list); // Sort ascending
Collections.sort(list, Collections.reverseOrder()); // Sort descending
ArrayList Time Complexity
Heap definition
A heap is a specialized tree based structure data structure that satisfies the heap property: if A is a parent node of B, then the key (the value) of node A is ordered with respect to the key of node B with the same ordering applying across the entire heap.
A heap can be classified further as either a “max heap” or a “min heap”. In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node.
Heap
PriorityQueue pq = new PriorityQueue<>(); // Min heap by default
PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder);
Heap Time Complexity
Queue definition
A Queue is a collection of elements, supporting two principle operations: enqueue, which inserts an element into the queue, and dequeue, which removes an element from the queue.
Queue
Queue q = new LinkedList<>(); // Specify as a LinkedList!
Queue Time Complexity