Java Arrays & StringBuilder Cheat Sheet

Essential methods for Array, StringBuilder, and Character Array manipulation

Java Array Methods (java.util.Arrays Class)

asList(T... a)
Arguments: Varargs

Returns a fixed-size list backed by the specified array.

binarySearch(array, key)
Arguments: 2

Searches for a specified element using the binary search algorithm (requires sorted array).

compare(array1, array2)
Arguments: 2

Lexicographically compares two arrays.

copyOf(original, newLength)
Arguments: 2

Copies the specified array, truncating or padding with default values as necessary.

copyOfRange(original, from, to)
Arguments: 3

Copies a range of the specified array into a new array.

deepEquals(a1, a2)
Arguments: 2

Checks if two multi-dimensional arrays are deeply equal.

deepHashCode(Object[] a)
Arguments: 1

Returns a deep hash code for the array (for multi-dimensional arrays).

deepToString(Object[] a)
Arguments: 1

Returns a deep string representation of the array (for multi-dimensional arrays).

equals(array1, array2)
Arguments: 2

Checks if two arrays are equal (same elements in same order).

fill(array, value)
Arguments: 2

Assigns the specified value to each element of the array.

hashCode(array)
Arguments: 1

Returns a hash code based on the contents of the array.

mismatch(array1, array2)
Arguments: 2

Finds and returns the index of the first mismatch between two arrays.

parallelPrefix(array, operator)
Arguments: 2

Applies a binary operator to each element in the array in parallel (e.g., cumulative sum).

parallelSetAll(array, generator)
Arguments: 2

Sets all elements in parallel using a generated function.

parallelSort(array)
Arguments: 1

Sorts the specified array in parallel (uses multiple threads for large arrays).

setAll(array, generator)
Arguments: 2

Sets each element using a generated function.

sort(array)
Arguments: 1

Sorts the specified array into ascending order.

spliterator(array)
Arguments: 1

Returns a spliterator over the elements in the array (Java 8+).

stream(array)
Arguments: 1

Returns a sequential Stream with the specified array as its source (Java 8+).

toString(array)
Arguments: 1

Returns a string representation of the array.

Note: The length property is used to get the size of an array in Java, but it is a field — not a method.

Java StringBuilder Methods

append(value)
Arguments: Various

Appends the specified value (string, char, int, etc.) to the builder.

insert(int offset, value)
Arguments: 2

Inserts a value at the specified offset position.

replace(int start, int end, String str)
Arguments: 3

Replaces the characters between start (inclusive) and end (exclusive) positions with the provided string.

delete(int start, int end)
Arguments: 2

Removes characters between the given start (inclusive) and end (exclusive) indexes.

deleteCharAt(int index)
Arguments: 1

Removes the character at the specified index.

reverse()
Arguments: 0

Reverses the sequence of characters.

capacity()
Arguments: 0

Returns the current capacity of the StringBuilder.

ensureCapacity(int minimumCapacity)
Arguments: 1

Increases the capacity, if needed, to ensure it can hold a minimum number of characters.

charAt(int index)
Arguments: 1

Returns the character at the specified index.

setCharAt(int index, char ch)
Arguments: 2

Sets the character at the specified index.

length()
Arguments: 0

Returns the length of the character sequence.

setLength(int newLength)
Arguments: 1

Sets the new length of the builder. If newLength is less than current length, characters are truncated. If greater, null characters are appended.

substring(int start)
Arguments: 1

Returns a new String that is a substring starting from the specified index to the end.

substring(int start, int end)
Arguments: 2

Returns a new String that is a substring from start (inclusive) to end (exclusive).

toString()
Arguments: 0

Converts the builder object to an immutable String.

trimToSize()
Arguments: 0

Trims the capacity to the current length. Reduces memory usage if capacity is much larger than actual content.

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Arguments: 4

Copies characters from this sequence into the destination character array.

indexOf(String str)
Arguments: 1

Returns the index of the first occurrence of the specified substring.

lastIndexOf(String str)
Arguments: 1

Returns the last occurrence of the specified substring.

offsetByCodePoints(int index, int codePointOffset)
Arguments: 2

Returns the index within this sequence that is offset by `codePointOffset` code points starting from `index`.

codePointAt(int index)
Arguments: 1

Returns the Unicode code point at the specified index.

codePointBefore(int index)
Arguments: 1

Returns the Unicode code point before the specified index.

codePointCount(int beginIndex, int endIndex)
Arguments: 2

Returns the number of Unicode code points between two indices.

subSequence(int start, int end)
Arguments: 2

Returns a new character sequence from start (inclusive) to end (exclusive).

Note: StringBuilder performs better than String for many string modifications due to its mutability.

Java Character Array Methods (char[])

Common Operations:

  • Declaration: char[] letters;
  • Initialization: char[] vowels = {'a', 'e', 'i', 'o', 'u'};
  • Access: letters[i]
  • Iteration (for loop): for (int i = 0; i < arr.length; i++) { ... }
  • Iteration (enhanced for loop): for (char c : arr) { ... }
  • Modification: arr[2] = 'z';
  • Comparison: Arrays.equals(arr1, arr2);
  • Sorting: Arrays.sort(arr);
  • Printing (as String): System.out.println(Arrays.toString(arr));
  • Conversion from String: char[] array = str.toCharArray();
  • Conversion to String: String s = new String(arr);

Utility Methods from java.util.Arrays:

equals(char[] a, char[] a2)
Arguments: 2

Checks if two character arrays are equal.

copyOf(char[] original, int newLength)
Arguments: 2

Copies the specified character array, truncating or padding as necessary.

copyOfRange(char[] original, int from, int to)
Arguments: 3

Copies a range of the specified character array into a new array.

fill(char[] a, char val)
Arguments: 2

Fills the character array with the specified value.

sort(char[] a)
Arguments: 1

Sorts the character array in ascending order.

toString(char[] a)
Arguments: 1

Returns a string representation of the character array.

binarySearch(char[] a, char key)
Arguments: 2

Searches the array for a key, returns the index or negative insertion point (requires sorted array).

Tip: Character arrays are often used for manual string manipulation, conversion, or when working with sensitive data (since char arrays can be cleared from memory).