LAST UPDATED: MARCH 5, 2021
Java Program To Split an Array from Specified Position
In this tutorial, we will learn how to split an array from a specified position. This means now the array will divide into two separate arrays. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: 8 7 9 5 4 3 1 6 0 9 8 3 4
Output: Position = 5
Array 1: 8 7 9 5 4
Array 2: 3 1 6 0 9 8 3 4
Program 1: Split an Array from a Specified Position
In this method, we will see how to split an array from a specified position using loops.
Algorithm
- Start
- Declare the size of the array.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array.
- Input the position from where you want to split the array.
- Declare two more arrays to store the divided arrays.
- Copy the elements to the specified position to one array.
- Copy the next half of the elements to another array.
- Display both the array.
- Stop.
The below program demonstrates how to split an array from a specified position using loops.
/*Java program to split an array from a specified position*/
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the position of the array : ");
int pos=sc.nextInt(); //Enter the position from where you want to split the array
int brr[]=new int[pos]; //Array to store the first half
int z=n-pos;
int crr[]=new int[z]; //Array to store the second half of the array
//Display the elements of the original array
System.out.println("Original Array Elements are ");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println("");
//Copy the first half elements
for(int i=0;i<pos;i++)
{
brr[i]=arr[i];
}
//Print the first half elements
System.out.println("The first array elements are : ");
for(int i=0;i<pos;i++)
{
System.out.print(brr[i]+ " ");
}
System.out.println("");
//Copy the second half elements
int k=0;
for(int i=pos;i<n;i++)
{
crr[k]=arr[i];
k++;
}
//Print the second half elements
System.out.println("The second array elements are : ");
for(int t=0;t<z;t++)
{
System.out.print(crr[t]+ " ");
}
}
}
Enter the total number of elements 10
Enter the elements of the array 2 3 4 1 5 6 7 2 8 9
Enter the position of the array: 4
Original Array Elements are
2 3 4 1 5 6 7 2 8 9
The first array elements are :
2 3 4 1
The second array elements are :
5 6 7 2 8 9
Program 2: Split an Array from a Specified Position
In this method, we will see how to split an array from a specified position using Arrays.copyofRange()
method.
Algorithm
- Start
- Declare the size of the array.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array.
- Input the position from where you want to split the array.
- Declare two more arrays to store the divided arrays.
- Copy the elements to the specified position to one array using
Arrays.copyofRange()
method.
- Copy the next half of the elements to another array using
Arrays.copyofRange()
method.
- Display both the array.
- Stop.
The below program demonstrates how to split an array from a specified position using Arrays.copyofRange()
method.
/*Java program to split an array from a specified position*/
import java.util.*;
import java.util.Arrays;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the position of the array : ");
int pos=sc.nextInt(); //Enter the position from where you want to split the array
int brr[]=new int[pos]; //Array to store the first half
int z=n-pos;
int crr[]=new int[z]; //Array to store the second half of the array
//Display the elements of the original array
System.out.println("Original Array Elements are ");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println("");
//Copy the first half elements
brr=Arrays.copyOfRange(arr, 0, pos);
//Print the first half elements
System.out.println("The first array elements are : ");
for(int i=0;i<pos;i++)
{
System.out.print(brr[i]+ " ");
}
System.out.println("");
//Copy the second half elements
crr=Arrays.copyOfRange(arr, pos, n);
//Print the second half elements
System.out.println("The second array elements are : ");
for(int t=0;t<z;t++)
{
System.out.print(crr[t]+ " ");
}
}
}
Enter the total number of elements 10
Enter the elements of the array 4 5 8 3 1 4 6 7 3 2
Enter the position of the array : 6
Original Array Elements are
4 5 8 3 1 4 6 7 3 2
The first array elements are :
4 5 8 3 1 4
The second array elements are :
6 7 3 2