- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JAVA Program to Replace Each Element of Array with its Next Element
In Java, Array is an object. It is a non-primitive data type which stores values of similar data types.
As per the problem statement we have to replace each element of the array with its next element and we can replace the last element with the first element.
Let’s start!
To show you some instances −
Instance-1
Suppose the original array is {2, 3, 1, 4, 6} After updating the array with its next element: {3, 1, 4, 6, 2}
Instance-2
Suppose the original array is {12, 23, 11, 64} After updating the array with its next element: {23, 11, 64, 12}
Instance-3
Suppose the original array is {11, 22, 33, 44, 55} After updating the array with its next element: {22, 33, 44, 55, 11}
Algorithm
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.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array Elements
By Using User Defined Method.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array Elements
In this approach, array elements will be initialized in the program. Then as per the algorithm replace the array element with its next element.
Example
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)); } }
Output
Array elements are: [4, 2, 3, 1] Updated array: [2, 3, 1, 4]
Approach-2: By Using User Defined Method
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm replace the array element with its next element.
Example
import java.util.Arrays; public class Main { public static void main(String args[]){ //Declare and initialize the array elements int arr[] = {4,2,3,1}; replaceElement(arr); } public static void replaceElement(int[] arr) { //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)); } }
Output
Array elements are: [4, 2, 3, 1] Updated array: [2, 3, 1, 4]
In this article, we explored how to replace each element of the array with its next element in Java.