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.
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!

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

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...

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,...

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...

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 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 use access_token of OAuth2.0 to restrict interface access permissions How to ensure access_token when authorizing using OAuth2.0...

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.