Quiz Entry - updated: 2026.12.02
Merge-sort's partition returns two sequences at once. How do you return two arrays from one Java method?
A Java method returns exactly one value, so the pair has to be wrapped into one object. In current Java the clean way is a record such as record Partitions(int[] s1, int[] s2) {}; older code uses a small wrapper class or an array of two arrays. Python simply returns a tuple.
Merge-sort's partition step hands back both halves at once, S1 and S2. Python can return several values from one function; Java cannot: a method returns void or exactly one value. The options:
| Option | Example | Verdict |
|---|---|---|
| array of arrays | return new int[][] { s1, s2 }; |
works, but nothing says it holds exactly two entries |
| wrapper class | class PartitionResult { int[] s1, s2; } |
clean, a few lines of boilerplate |
| record (Java 16+) | record Partitions(int[] s1, int[] s2) {} |
the same wrapper in one line, read-only, with accessors s1() and s2() |
record Partitions(int[] s1, int[] s2) {}
static Partitions partition(int[] s, int length) {
int[] s1 = new int[length];
int[] s2 = new int[s.length - length];
System.arraycopy(s, 0, s1, 0, length);
System.arraycopy(s, length, s2, 0, s2.length);
return new Partitions(s1, s2);
}
In Python the same function ends with return s[:n], s[n:], and the caller unpacks the tuple with s1, s2 = partition(s, len(s) // 2).
Go deeper:
Record classes (Java 21 language guide) — what a record generates for you: constructor, accessors,
equals,hashCodeandtoString.