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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

Regarding the analysis method of IntelliJIDEA cracking in the programming world, IntelliJ...

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Exploring the application of ultimate consistency in distributed systems Distributed transaction processing has always been a problem in distributed system architecture. To solve the problem...

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...
