Java Program to Split an Array from Specified Position
Last Updated :
26 Nov, 2024
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
// 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
// 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
// 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.
Similar Reads
Java Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave-like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
4 min read
Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
Java Program to Split the array and add the first part to the end | Set 2 Given an array and split it from a specified position, and move the first part of array add to the end.  Examples:  Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2 Output : arr[] = {5, 6, 52, 36, 12, 10} Explanation : Split from index 2 and first part {12, 10} add to the end . Input : arr[] = {3, 1,
2 min read
Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by "
5 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read
Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E
3 min read
Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
2 min read
How to Extract a Specific Line from a Multi-Line String in Java? In Java, String Plays an important role. As String stores or we can say it is a collection of characters. We want to get a specific line from a multi-line String in Java. This can also be done in Java. In this article, we will be learning how to extract a specific line from a multi-line String in Ja
2 min read
Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr
2 min read
Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided
2 min read