Java Program to Divide an Arrays in Two Arrays

In the previous article, we have seen Java Program to Copy an Array in Reverse

In this article we are going to see how we can divide an array into two subarrays in JAVA.

Java Program to Divide an Arrays in Two Arrays

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to divide an array into two subarrays.

Method-1: Java Program to Divide an Arrays in Two Arrays by Find Mid Index and By Copying Elements Individually

Approach:

  • Create an array.
  • Display the array to the user.
  • Find the mid index of the array
  • Create two subarrays. In the first sub array set size to mid, and for the second subtract the size of the first sub array from the size of the main array.
  • Use a for loop to copy elements into the subarrays.
  • Print both the sub arrays.

Program:

import java.util.Arrays;
import java.util.Collections;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54};
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(arr));
        int len = arr.length;
        // Creating the subarrays
        int subArr1[] = new int[(len+1)/2];
        int subArr2[] = new int[len-subArr1.length];

        // Copying elements to sub arrays individually
        for (int i = 0; i < len; i++)
        {
            if (i < subArr1.length) {
                subArr1[i] = arr[i];
            }
            else {
                subArr2[i - subArr1.length] = arr[i];
            }
        }
        // Printing the sub arrays
        System.out.println("The sub array 1 elements are : "+Arrays.toString(subArr1));
        System.out.println("The sub array 2 elements are : "+Arrays.toString(subArr2));
    }
}
Output:

The array elements are : [12, 22, 34, 22, 54]
The sub array 1 elements are : [12, 22, 34]
The sub array 2 elements are : [22, 54]

Method-2: Java Program to Divide an Arrays in Two Arrays by Find Mid Index and By Using System.arraycopy() Method

Approach:

  • Create an array.
  • Display the array to the user.
  • Find the mid index of the array
  • Create two subarrays. In the first sub array set size to mid, and for the second subtract the size of the first sub array from the size of the main array.
  • Use System.arraycopy to copy the elements.
  • Print both the sub arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54};
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(arr));
        int len = arr.length;
        // Creating the subarrays
        int subArr1[] = new int[(len+1)/2];
        int subArr2[] = new int[len-subArr1.length];

        // Copying elements to sub arrays
        System.arraycopy(arr, 0, subArr1, 0, subArr1.length);
        System.arraycopy(arr, subArr1.length, subArr2, 0, subArr2.length);
        // Printing the sub arrays
        System.out.println("The sub array 1 elements are : "+Arrays.toString(subArr1));
        System.out.println("The sub array 2 elements are : "+Arrays.toString(subArr2));
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54]
The sub array 1 elements are : [12, 22, 34]
The sub array 2 elements are : [22, 54]

Method-3: Java Program to Divide an Arrays in Two Arrays by Find Mid Index and By Uing copyOfRange() Method

Approach:

  • Create an array.
  • Display the array to the user.
  • Find the mid index of the array
  • Create two subarrays. In the first sub array set size to mid, and for the second subtract the size of the first sub array from the size of the main array.
  • Use Arrays.copyOfRange() to copy the elements.
  • Print both the sub arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54};
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(arr));
        int len = arr.length;
        // Creating the subarrays and copying elements
        int subArr1[] = Arrays.copyOfRange(arr,0,(len+1)/2);
        int subArr2[] = Arrays.copyOfRange(arr,(len+1)/2,len);

        // Printing the sub arrays
        System.out.println("The sub array 1 elements are : "+Arrays.toString(subArr1));
        System.out.println("The sub array 2 elements are : "+Arrays.toString(subArr2));
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54]
The sub array 1 elements are : [12, 22, 34]
The sub array 2 elements are : [22, 54]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: