In Java, splitting an array means dividing the array into two parts based on a given position. This operation creates two new arrays that represent the segments before and after the given index.
Example:
The simplest way to split an array in Java from a specified position is by using the in-built Arrays.copyOfRange() method.
// Java Program to Split Array
// Using Arrays.copyOfRange()
import java.util.Arrays;
public class SplittingArray {
public static void main(String args[]) {
// Original Array
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int n = a.length;
// Position to split the array
int p = 5;
// Validate the split position to ensure
// it's within array bounds
if (p > 0 && p < n) {
int b[] = new int[p];
int c[] = new int[n - p];
// Initialize array "b" with elements from index 0 to p - 1
b = Arrays.copyOfRange(a, 0, p);
// Initialize array "c" with elements from index p to n - 1
c = Arrays.copyOfRange(a, p, n);
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));
}
else {
System.out.println("Invalid position.");
}
}
}
Output
[1, 2, 3, 4, 5] [6, 7, 8, 9, 0]
Other Ways to Split an Array from Specified Position
Using a Single for loop
This method is more efficient solution because it only requires one pass over the array. When we want to split an array at a specified position with minimal complexity, this approach is optimal, compared to using multiple loops or in-built methods.
// Java Program to Split Array
// using only one for loop
import java.util.Arrays;
public class SplittingArray2 {
public static void main(String args[]) {
// original array
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int n = a.length;
int p = 5;
if (p > 0 && p < n) {
int b[] = new int[p];
int c[] = new int[n - p];
// only using one for loop to split the array into b and c
for (int i = 0; i < n; i++) {
if (i < p) {
b[i] = a[i];
} else {
c[i - p] = a[i];
}
}
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));
}
else {
System.out.println("Invalid position.");
}
}
}
Output
[1, 2, 3, 4, 5] [6, 7, 8, 9, 0]
Explanation: First, we declare two arrays "b" and "c" with sizes "p" and "n - p", respectively. Then, we use a single loop to fill both arrays based on the index position.
Using Two for Loops
This method uses separate loops to fill each new array. This is less efficient than a single-loop approach. It can be useful when readability is prioritized over performance specially for smaller arrays.
// Java Program to Split Array
// Using two for loops
import java.util.Arrays;
public class SplittingArray3 {
public static void main(String args[]) {
// Original array
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int n = a.length;
int p = 5;
// validating the position for invalid values
if (p > 0 && p < n) {
// declaring array b and c
int b[] = new int[p];
int c[] = new int[n - p];
for (int i = 0; i < p; i++) {
b[i] = a[i];
}
for (int i = 0; i < n - p; i++) {
c[i] = a[i + p];
}
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));
}
else {
System.out.println("Invalid position.");
}
}
}
Output
[1, 2, 3, 4, 5] [6, 7, 8, 9, 0]
Explanation: First, we declare two arrays b and c with sizes p and n - p, respectively. Then we use two loops, the first loop runs from 0 to p, initializing array b. The second loop runs from 0 to n - p ,initializing array c.