
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to Alter Two Array Elements in Java
An array is a linear data structure in which elements are stored in contiguous memory locations.
As per problem statement, we have to alter two array elements with each other. Altering two array elements in other words can also be called swapping or exchanging two elements with each other
Let’s explore the article to see how it can be done by using Java programming language.
To Show you Some Instances
Instance-1
Suppose we have the below array = [10, 2, 3, -5, 99, 12, 0, -1]
Now if we swap the 5th and 8th elements,
Then, we have the new array [10, 2, 3, -5, -1, 12, 0, 99]
Instance-2
Suppose we have the below array = [55, 10, 29, 74, 12, 45, 6, 5, 269]
Now if we swap the 4th and 8th elements
Then, we have the new array [55, 10, 29, 5, 12, 45, 6, 74, 269]
Instance-3
Suppose we have the below array = [556, 10, 259, 874, 123, 453, -96, -54, -2369]
Now if we swap the 2nd and 6th elements
Then, we have the new array [556, 453, 259, 874, 123, 10, -96, -54, -2369]
Algorithm
Algorithm-1 (By Using a Third Variable)
Step 1 − After storing the arrays, take two indices to swap elements.
Step 2 − Store the first element in a temporary variable.
Step 3 − Now store the second element value in first element
Step 4 − Finally store the temporary variable value in the second element.
Step 5 − Print the array elements.
Algorithm-2 (Without Using a Third Variable)
Step 1 − After storing the arrays, take two indices to swap elements.
Step 2 − Add first and second element then store them in first element.
Step 3 − Subtract the second element from the first element and store it in the second element.
Step 4 − Again, subtract the second element from the first element and store in first element.
Step 5 − Print the array elements.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
You can use Arrays.sort() method to sort the array in ascending order.
Arrays.sort(array_name);
Multiple Approaches
We have provided the solution in different approaches.
Altering Two Array Elements Using Third Variable.
Altering Two Array Elements Without Using Third Variable.
Let’s see the program along with its output one by one.
Approach-1: By Using a Third variable
In this approach, we alter the array elements by using another variable that temporarily holds the value of one element.
Example
import java.io.*; import java.util.Arrays; public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 }; // Print all array elements System.out.println("The array elements before swapping are-"); for (int i : arr) { System.out.print(i + " "); } // We will be swapping 2nd index element with 4th index element int firstIndex = 2, secondIndex = 4; // Temp variable int temp = arr[firstIndex]; // Swapping arr[firstIndex] = arr[secondIndex]; arr[secondIndex] = temp; // Print all array elements System.out.println("\nThe array elements after swapping are-"); for (int i : arr) { System.out.print(i + " "); } } }
Output
The array elements before swapping are- 10 2 3 -5 99 12 0 -1 The array elements after swapping are- 10 2 99 -5 3 12 0 -1
Approach-2: Without Using a Third Variable
In this approach, we alter the array elements without using another variable unlike the previous method.
Example
import java.io.*; import java.util.Arrays; public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 }; // Print all array elements System.out.println("The array elements before swapping are-"); for (int i : arr) { System.out.print(i + " "); } // We will be swapping 2nd index element with 4th index element int firstIndex = 2, secondIndex = 4; // Swapping array elements arr[firstIndex] = arr[firstIndex] + arr[secondIndex]; arr[secondIndex] = arr[firstIndex] - arr[secondIndex]; arr[firstIndex] = arr[firstIndex] - arr[secondIndex]; // Print all array elements System.out.println("\nThe array elements after swapping are-"); for (int i : arr) { System.out.print(i + " "); } } }
Output
The array elements before swapping are- 10 2 3 -5 99 12 0 -1 The array elements after swapping are- 10 2 99 -5 3 12 0 -1
In this article, we explored how to alter two array elements with each other by using Java programming language.
- Related Articles
- How to swap two array elements in JavaScript?
- Find Two Array Elements Having Maximum Product in Java?
- Find Two Array Elements Having Maximum Sum in Java?
- Find sum of two array elements index wise in Java
- How to make elements of array immutable in Java?
- How to sort Java array elements in ascending order?
- How to use JOptionPane with Array Elements in Java?
- How to count unique elements in the array using java?
- How to remove duplicate elements of an array in java?
- How to add elements to the midpoint of an array in Java?
- How to create JTable from two dimensional array in Java?
- How to reverse the elements of an array using stack in java?
- How to sort an array of objects containing null elements in java?
- Arrange Negative Array Elements Before Positive Elements in Java
- C++ program to find array after inserting new elements where any two elements difference is in array
