Home  >  Article  >  Java  >  How to reverse a Java array?

How to reverse a Java array?

藏色散人
藏色散人Original
2019-04-09 17:14:0810392browse

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