- 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
How to move an array element from one array position to another in Java?
To move an element from one position to other (swap) you need to –
- Create a temp variable and assign the value of the original position to it.
- Now, assign the value in the new position to original position.
- Finally, assign the value in the temp to the new position.
Example
import java.util.Arrays; public class ChangingPositions { public static void main(String args[]) { int originalPosition = 1; int newPosition = 4; int [] myArray = {23, 93, 56, 92, 39}; int temp = myArray[originalPosition]; myArray[originalPosition] = myArray[newPosition]; myArray[newPosition] = temp; System.out.println(Arrays.toString(myArray)); } }
Output
[23, 39, 56, 92, 93]
- Related Articles
- Can i refer an element of one array from another array in java?
- How to move an element of an array to a specific position (swap)?
- How to move (translate) a JavaFX node from one position to another?
- How to remove an element from an array in Java
- Java Program to Split an Array from Specified Position
- How can we copy one array from another in Java
- How do you copy an element from one list to another in Java?
- Move different elements to another array in MongoDB?
- How to print data of specific element from an array in java?
- How to create a subarray from another array in Java
- Copy values from one array to another in Numpy
- How to create an array of partial objects from another array in JavaScript?
- How to filter an array from all elements of another array – JavaScript?
- How to delete element from an array in MongoDB?
- How to switch data from an array to array list in java?

Advertisements