Replace Each Element of Array with Its Next Element in Java



As per the problem statement we have to write a Java program to replace each element of the array with its next element. In Java, the array is an object. It is a non-primitive data type which stores values of similar data types.

Before discussing the solution for the given problem, let's understand the problem statement with an example ?

Example Scenario:

Input: arr[] = {12, 23, 11, 64} Output: updated array = {23, 11, 64, 12}

Algorithm

Follow the steps below to replace each element of the array with its next element ?

  • Step 1 ? Declare and initialize an integer array.
  • Step 2 ? Store the first element of the array in a temp variable to replace it with the last element.
  • Step 3 ? Replace the current element arr[i] with arr[i+1]. Continue this till arr[lastIndex-1]. And replace the last index element with temp value.
  • Step 4 ? Print the elements of the array.

Using for Loop

A for loop In Java is a control structure that allows you to repeat a block of code a specific number of times. Here, we use it to iterate through each element of the given array and replace the current element with next element.

Example

In this Java program, we are using for loop to replace each element of array with its next element.

Open Compiler
import java.util.Arrays; public class Main { // main method public static void main(String args[]) { //Declare and initialize the array elements int arr[] = {4,2,3,1}; //get the length of the array int size=arr.length; //assign the first element of the array to int variable 'temp' int temp=arr[0]; // Print the array elements System.out.println("Array elements are: " + Arrays.toString(arr)); //replace the current element with next element for(int i=0; i < (arr.length-1); i++) { arr[i]=arr[i+1]; } //replace the last element of the array with first element arr[size-1]=temp; //print the updated array System.out.println("Updated array: "+Arrays.toString(arr)); } }

On executing, following output will be displayed ?

Array elements are: [4, 2, 3, 1]
Updated array: [2, 3, 1, 4]

Using arraycopy() Method

In this approach, we use the arraycopy() method to copy elements from the original array to the new array, starting from the second element i.e. index 1 and placing them at the beginning i.e. index 0 of new array.

The arraycopy() method belongs to System class of java.lang package. This method copies elements from a source array, starting from the specified position, to the specified position of the destination array.

Example

Let's see the practical demonstration ?

Open Compiler
import java.util.Arrays; public class Main { public static void main(String[] args) { // given array int arr[] = {4, 2, 3, 1, 5}; // Print the array elements System.out.println("Array elements are: " + Arrays.toString(arr)); // new array int newArr[] = new int[arr.length]; // assign the first element of the array to a temp variable int temp = arr[0]; // replacing current element with next element System.arraycopy(arr, 1, newArr, 0, arr.length - 1); // replace the last element of the array with first element newArr[newArr.length - 1] = temp; // updated array System.out.print("Updated array: "); for (int j : newArr) { System.out.print(j + " "); } } }

On running, you will get the below output ?

Array elements are: [4, 2, 3, 1, 5]
Updated array: 2 3 1 5 4 
Updated on: 2024-09-11T10:56:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements