Java Arrays and String Methods - Cheat Sheet
Arrays Methods
1. sort(arr): Sorts the array in ascending order.
Example: Arrays.sort(arr);
2. binarySearch(arr, key): Returns index of key, or negative insertion point.
Example: Arrays.binarySearch(arr, 5);
3. fill(arr, val): Fills all elements with val.
Example: Arrays.fill(arr, 7);
4. equals(arr1, arr2): Returns true if arrays are equal.
Example: Arrays.equals(a, b);
5. copyOf(arr, newLength): Copies array to new length.
Example: Arrays.copyOf(arr, 3);
6. copyOfRange(arr, from, to): Copies a range.
Example: Arrays.copyOfRange(arr, 1, 3);
7. toString(arr): Returns string representation.
Example: Arrays.toString(arr);
8. deepToString(nested): For nested arrays.
Example: Arrays.deepToString(nested);
9. hashCode(arr): Returns hash of array.
Example: Arrays.hashCode(arr);
10. parallelSort(arr): Faster parallel sort.
Example: Arrays.parallelSort(arr);
11. setAll(arr, lambda): Fills with lambda result.
Example: Arrays.setAll(arr, i -> i*i);
String Methods
1. length(): Returns string length.
Example: s.length();
2. charAt(i): Returns char at index i.
Example: s.charAt(1);
3. substring(start, end): Substring of string.
Example: s.substring(1, 3);
4. equals(str): Compares strings.
Java Arrays and String Methods - Cheat Sheet
Example: s.equals("hello");
5. equalsIgnoreCase(str): Case-insensitive comparison.
Example: s.equalsIgnoreCase("HELLO");
6. compareTo(str): Lexicographical compare.
Example: "a".compareTo("b");
7. contains(seq): Checks for sequence.
Example: s.contains("ell");
8. indexOf(ch): Index of first occurrence.
Example: s.indexOf('l');
9. startsWith(prefix): Checks prefix.
Example: s.startsWith("he");
10. endsWith(suffix): Checks suffix.
Example: s.endsWith("lo");
11. toUpperCase()/toLowerCase(): Case conversion.
Example: s.toUpperCase();
12. trim(): Removes leading/trailing spaces.
Example: " hi ".trim();
13. replace(old, new): Replaces characters.
Example: s.replace('l', 'x');
14. split(regex): Splits into array.
Example: "a,b".split(",");
15. valueOf(val): Converts to string.
Example: String.valueOf(123);
16. isEmpty()/isBlank(): Checks emptiness (Java 11+ for isBlank).
Example: "".isEmpty(); " ".isBlank();