Java syntax 1 Flashcards
Create a HashMap
Map> map = new HashMap<>();
Create an array of ints
int[] freqArray = new int[128];
Loop through a list of strings (no index)
for (String s : strs) {
Loop through characters in a String (same type)
for (int i = 0; i < s.length(); i ++ { s.charAt(i) //to get val
Loop through characters in a String (char type)
for (char c : s.toCharArray()) {
Check if HashMap contains a key
map.containsKey(key);
Insert a value into a HashMap at a specified key
map.put(key, value);
Get a list of all a HashMap’s values
new ArrayList<>(map.values());
Create an ArrayList of type String
ArrayList ex = new ArrayList();
Create a List of type Integer
List list = new ArrayList<>();
Get and Set elements in Array vs ArrayList
Array -> arr[0], arr[1], arr[0] = 1, arr[1] = 2
ArrayList -> arrL.add(1); arrL.add(2);
Get a char at index i in a String
s.charAt(i)
If var is null -> 0, else set to something
int x = (p != null) ? p.val : 0;
Sort a int[][] by the first value in each sub []
Arrays.sort(slots1, (a, b) -> a[0] - b[0]);