Home > Java > javaTutorial > How to modify two array elements in Java

How to modify two array elements in Java

PHPz
Release: 2023-09-13 19:45:03
forward
1213 people have browsed it

How to modify two array elements in Java

An array is a linear data structure in which elements are stored in contiguous memory locations.

According to the problem statement, we have to change two array elements with each other. In other words, changing two array elements can also be called swapping or swapping two elements

Let’s explore this article and see how it can be done using the Java programming language.

Show you some examples

Example 1

Suppose we have the following array = [10, 2, 3, -5, 99, 12, 0, -1]

Now if we swap the 5th and 8th elements,

Then, we got the new array [10, 2, 3, -5, -1, 12, 0, 99]

Example 2

Suppose we have the following array = [55, 10, 29, 74, 12, 45, 6, 5, 269]

Now if we swap the fourth and eighth elements

Then, we got the new array [55, 10, 29, 5, 12, 45, 6, 74, 269]

Example 3

Suppose we have the following array = [556, 10, 259, 874, 123, 453, -96, -54, -2369]

Now if we swap the second and sixth elements

Then, we got the new array [556, 453, 259, 874, 123, 10, -96, -54, -2369]

algorithm

Algorithm 1 (by using the third variable)

  • Step 1 - After storing the array, take two indices to swap the elements.

  • Step 2 - Store the first element in a temporary variable.

  • Step 3 - Now store the second element value in the first element

  • Step 4 - Finally store the temporary variable value in the second element.

  • Step 5 - Print the array elements.

Algorithm 2 (without using the third variable)

  • Step 1 - After storing the array, take two indices to swap the elements.

  • Step 2 - Add the first and second elements and store them in the 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 it in the first element.

  • Step 5 - Print the array elements.

grammar

To get the length of an array (the number of elements in the array), arrays have a built-in property, length.

The following is its syntax -

array.length
Copy after login

Among them, "array" refers to the array reference.

You can use the Arrays.sort() method to sort an array in ascending order.

Arrays.sort(array_name);
Copy after login

Multiple methods

We provide solutions in different ways.

  • Use a third variable to change two array elements.

  • Change two array elements without using a third variable.

Let’s look at the program and its output one by one.

Method 1: Use the third variable

In this method, we change the array element by using another variable that temporarily holds the value of an 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 + " ");
      }
   }
}
Copy after login

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 
Copy after login
Copy after login

Method 2: Do not use the third variable

In this method, unlike the previous method, we can change the array elements without using other variables.

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 + " ");
      }
   }
}
Copy after login

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 
Copy after login
Copy after login

In this article, we explored how to change two array elements using the Java programming language.

The above is the detailed content of How to modify two array elements in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template