Java Array Methods (java.util.Arrays
Class)
Returns a fixed-size list backed by the specified array.
Searches for a specified element using the binary search algorithm (requires sorted array).
Lexicographically compares two arrays.
Copies the specified array, truncating or padding with default values as necessary.
Copies a range of the specified array into a new array.
Checks if two multi-dimensional arrays are deeply equal.
Returns a deep hash code for the array (for multi-dimensional arrays).
Returns a deep string representation of the array (for multi-dimensional arrays).
Checks if two arrays are equal (same elements in same order).
Assigns the specified value to each element of the array.
Returns a hash code based on the contents of the array.
Finds and returns the index of the first mismatch between two arrays.
Applies a binary operator to each element in the array in parallel (e.g., cumulative sum).
Sets all elements in parallel using a generated function.
Sorts the specified array in parallel (uses multiple threads for large arrays).
Sets each element using a generated function.
Sorts the specified array into ascending order.
Returns a spliterator over the elements in the array (Java 8+).
Returns a sequential Stream with the specified array as its source (Java 8+).
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
Appends the specified value (string, char, int, etc.) to the builder.
Inserts a value at the specified offset position.
Replaces the characters between start (inclusive) and end (exclusive) positions with the provided string.
Removes characters between the given start (inclusive) and end (exclusive) indexes.
Removes the character at the specified index.
Reverses the sequence of characters.
Returns the current capacity of the StringBuilder.
Increases the capacity, if needed, to ensure it can hold a minimum number of characters.
Returns the character at the specified index.
Sets the character at the specified index.
Returns the length of the character sequence.
Sets the new length of the builder. If newLength is less than current length, characters are truncated. If greater, null characters are appended.
Returns a new String that is a substring starting from the specified index to the end.
Returns a new String that is a substring from start (inclusive) to end (exclusive).
Converts the builder object to an immutable String.
Trims the capacity to the current length. Reduces memory usage if capacity is much larger than actual content.
Copies characters from this sequence into the destination character array.
Returns the index of the first occurrence of the specified substring.
Returns the last occurrence of the specified substring.
Returns the index within this sequence that is offset by `codePointOffset` code points starting from `index`.
Returns the Unicode code point at the specified index.
Returns the Unicode code point before the specified index.
Returns the number of Unicode code points between two indices.
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
:
Checks if two character arrays are equal.
Copies the specified character array, truncating or padding as necessary.
Copies a range of the specified character array into a new array.
Fills the character array with the specified value.
Sorts the character array in ascending order.
Returns a string representation of the character array.
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).