Java Program to Move An Array Element From One Array Position to Another Position

In the previous article, we have seen Java Program to Find Number of 1’s in an Integer Array

In this article we will see how to move an array element from one array position to another position using Java programming language.

Java Program to Move An Array Element From One Array Position to Another Position

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Let’s see different ways to move an array element from one array position to another position.

Method-1: Java Program to Move An Array Element From One Array Position to Another Position By Using an Extra Variable

Approach:

  • Create an array with elements which is the original array i.e arr[].
  • Now declare the two indices, elements of which needs to be swapped say swapIndex1 and swapIndex2.
  • Declare an 3rd variable say ‘temp‘, then assign value of swapIndex1
  • Then to swapIndex1 assign the value of swapIndex2
  • Now to swapIndex2 assign the value of temp.
  • Print the new array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        //Array declared with array elements
       int arr[] ={1,2,3,1,4,5,1,6};
        
        System.out.print("Original Array before swapping: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
        //Declared two indices
        //The elements present at those two indices will be swapped
        //means element from one array position will be moved to another array position
        int swapIndex1=2;
        int swapIndex2=5;
        
        //Moving elements from one position to another position
        //here we will move present at index-2 to index-5 and element present at index-5 to index-2
        //swapped using the help of a 3rd variable say 'temp'
        int temp=arr[swapIndex1];
        arr[swapIndex1]=arr[swapIndex2];
        arr[swapIndex2]=temp;
        
         System.out.print("Modified Array after swapping: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
    }
}
Output:

Original Array before swapping: 1 2 3 1 4 5 1 6 
Modified Array after swapping: 1 2 5 1 4 3 1 6

Method-2: Java Program to Move An Array Element From One Array Position to Another Position Without Using an Extra Variable

Approach:

  • Create an array with elements which is the original array i.e arr[].
  • Now declare the two indices, elements of which needs to be swapped say swapIndex1 and swapIndex2.
  • Then swap array elements without using an extra variable.
  • Print the new array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        //Array declared with array elements
       int arr[] ={1,2,3,1,4,5,1,6};
        
        System.out.print("Original Array before swapping: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
        //Declared two indices
        //The elements present at those two indices will be swapped
        //means element from one array position will be moved to another array position
        int swapIndex1=2;
        int swapIndex2=5;
        
        //without using 3rd variable
        arr[swapIndex1]=arr[swapIndex1]+arr[swapIndex2];
        arr[swapIndex2]=arr[swapIndex1]-arr[swapIndex2];
        arr[swapIndex1]=arr[swapIndex1]-arr[swapIndex2];
        
         System.out.print("Modified Array after swapping: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
    }
}
Output:

Original Array before swapping: 1 2 3 1 4 5 1 6 
Modified Array after swapping: 1 2 5 1 4 3 1 6

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Articles: