Arrays Utility Deep Dive
The java.util.Arrays class is the Swiss-army knife for array manipulation. Below are the most common operations you will use in day-to-day development.
int[] data = {7, 3, 9, 1};
System.out.println(Arrays.toString(data)); // [7, 3, 9, 1]
int[] slice = Arrays.copyOfRange(data, 1, 3); // [3, 9]
int[] padded = Arrays.copyOf(data, 6); // [7, 3, 9, 1, 0, 0]
double[] price = {100, 80, 120};
Arrays.setAll(price, idx -> price[idx] * 0.9); // apply 10 % discount
Arrays.sort(price); // [72.0, 90.0, 108.0]
Sorting Custom Objects
When the array contains user-defined types, you must supply the ordering logic.
class Player implements Comparable<Player> {
String nick;
int score;
public int compareTo(Player o) { // natural ordering by score
return Integer.compare(this.score, o.score);
}
}
Player[] lobby = {
new Player("Alice", 1200),
new Player("Bob", 800)
};
Arrays.sort(lobby); // natural order
Arrays.sort(lobby, (p1, p2) -> p1.nick.compareTo(p2.nick)); // by name
Lambda Expresssions
Introduced in Java 8, lambdas provide a concise way to implement functional interfaces—interfaces with exactly one abstract method.
@FunctionalInterface
interface Task { void run(); }
Task t1 = () -> System.out.println("running");
t1.run();
Step-wise simplification:
(int x) -> { return x * 2; }(x) -> x * 2x -> x * 2
Method References
Method references are a further shorthand when a lambda only calls an existing method.
// Static reference
Arrays.sort(players, Comparator.comparingInt(Player::getScore));
// Instance reference
Comparator<Player> descByScore = Comparator.comparingInt(Player::getScore).reversed();
// Constructor reference
Supplier<Player> factory = Player::new;
Player p = factory.get();
Classic Algorithms
Bubble Sort
public static void bubble(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
Selection Sort
public static void selection(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) min = j;
}
int tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
}
Binary Search
public static int binary(int[] sorted, int key) {
int lo = 0, hi = sorted.length - 1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (sorted[mid] == key) return mid;
else if (sorted[mid] < key) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}
Regular Expressions
Regexes describe patterns for matching, searching, and manipulating text.
Quick Examples
boolean ok = qq.matches("[1-9]\\d{5,19}"); // QQ number
boolean mail = email.matches("\\w+@\\w+(\\.\\w+)+"); // e-mail
Common Constructs
| Pattern | Meaning |
|---|---|
[abc] |
a, b, or c |
\\d |
digit 0-9 |
\\w + |
one or more word chars |
{n,m} |
between n and m occurrences |
(?i) |
case-insensitive |
Capturing and Replacing
String raw = "abc123xyz456";
String cleaned = raw.replaceAll("\\d+", "-"); // abc-xyz-
String[] parts = raw.split("\\d+"); // [abc, xyz]
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(raw);
while (m.find()) {
System.out.println(m.group()); // 123 456
}