search
HomeJavajavaTutorialHow to reverse a Java array?

This article will introduce to you how to reverse a Java array. Then reversing an array in Java can be done in three simple ways.

How to reverse a Java array?

First method:

(1) Enter the size of the array and the elements of the array.

(2) Consider a function reverse, which takes parameters - an array (such as arr) and the size of the array (such as n).

(3) Inside the function, initialize a new array (the array size of the first array is arr). The array arr[] is iterated from the first element and each element of the array arr[] is placed in the new array from behind, i.e. the new array is iterated from its last element.

(4) In this way, all elements of the array arr[] are placed in the new array in reverse.

(5) Additionally, we can iterate over the new array from scratch and print the elements of the array.

public class reverseArray { 
     static void reverse(int a[], int n) 
    { 
        int[] b = new int[n]; 
        int j = n; 
        for (int i = 0; i < n; i++) { 
            b[j - 1] = a[i]; 
            j = j - 1; 
        } 
  
        System.out.println("反转数组: \n"); 
        for (int k = 0; k < n; k++) { 
            System.out.println(b[k]); 
        } 
    } 
  
    public static void main(String[] args) 
    { 
        int [] arr = {10, 20, 30, 40, 50}; 
        reverse(arr, arr.length); 
    } 
}

Output:

反转数组: 

50
40
30
20
10

Second method:

Use similar code to input and print arrays. However, we don't create a new array like the method above. Instead, we reverse the original array itself. In this method, we exchange the elements of the array. The first element is swapped with the last element. The second element id is swapped with the penultimate element, and so on.

For example, [1,2,3,...,n-2,n-1,n]. We swap 1 with n, 2 with n-1,3, and n-2.

public class arrayReverse { 
  
    static void reverse(int a[], int n) 
    { 
        int i, k, t; 
        for (i = 0; i < n / 2; i++) { 
            t = a[i]; 
            a[i] = a[n - i - 1]; 
            a[n - i - 1] = t; 
        } 
  
        System.out.println("反转数组: \n"); 
        for (k = 0; k < n; k++) { 
            System.out.println(a[k]); 
        } 
    } 
  
    public static void main(String[] args) 
    { 
        int [] arr = {10, 20, 30, 40, 50}; 
        reverse(arr, arr.length); 
    } 
}

Output:

反转数组: 

50
40
30
20
10

Third method:

Use function java.util.Collections.reverse(list of lists) method. This method reverses the elements in the specified list. So we first convert the array to a list using java.util.Arrays.asList(array) and then reverse the list.

import java.util.*; 
  
public class reversingArray { 
  
    static void reverse(Integer a[]) 
    { 
        Collections.reverse(Arrays.asList(a)); 
        System.out.println(Arrays.asList(a)); 
    } 
  
    public static void main(String[] args) 
    { 
        Integer [] arr = {10, 20, 30, 40, 50}; 
        reverse(arr); 
    } 
}

Output:

[50, 40, 30, 20, 10]

This article is an introduction to Java array reversal. I hope it will be helpful to friends in need!

The above is the detailed content of How to reverse a Java array?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Apr 19, 2025 pm 07:57 PM

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Apr 19, 2025 pm 07:54 PM

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

In Java programming, how to stop subsequent code execution when student ID is repeated?In Java programming, how to stop subsequent code execution when student ID is repeated?Apr 19, 2025 pm 07:51 PM

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Apr 19, 2025 pm 07:48 PM

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

After the Spring Boot service is running for a period of time, how to troubleshoot?After the Spring Boot service is running for a period of time, how to troubleshoot?Apr 19, 2025 pm 07:45 PM

The troubleshooting idea of ​​SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...

How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?Apr 19, 2025 pm 07:42 PM

How to push video streams from Hikvision camera SDK to front-end Vue project? During the development process, you often encounter videos that need to be captured by the camera to be circulated...

How to limit access to access_token via OAuth2.0 scope parameter?How to limit access to access_token via OAuth2.0 scope parameter?Apr 19, 2025 pm 07:39 PM

How to use access_token of OAuth2.0 to restrict interface access permissions How to ensure access_token when authorizing using OAuth2.0...

In Spring Boot Redis, how to solve the problem of returning garbled codes?In Spring Boot Redis, how to solve the problem of returning garbled codes?Apr 19, 2025 pm 07:36 PM

SpringBootRedis gets the key garbled problem analysis using Spring...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.