This question already has an answer here:
这个问题已经有了答案:
- How do I reverse an int array in Java? 38 answers
- 如何在Java中反转int数组?38个答案
If I have an array like this:
如果我有一个这样的数组:
1 4 9 16 9 7 4 9 11
What is the best way to reverse the array so that it looks like this:
使数组看起来像这样的最好方法是什么?
11 9 4 7 9 16 9 4 1
I have the code below, but I feel it is a little tedious:
我有下面的代码,但我觉得有点乏味:
public int[] reverse3(int[] nums) {
return new int[] { nums[8], nums[7], nums[6], nums[5], num[4],
nums[3], nums[2], nums[1], nums[0] };
}
Is there a simpler way?
有更简单的方法吗?
15 个解决方案
#1
69
Collections.reverse()
can do that job for you if you put your numbers in a List
of Integers
.
如果您将数字放在整数列表中,那么reverse()可以为您完成这项工作。
List<Integer> list = Arrays.asList(1, 4, 9, 16, 9, 7, 4, 9, 11);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
Output:
输出:
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 9, 4, 7, 9, 16, 9, 4, 1]
#2
59
If you want to reverse the array in-place:
如果你想反转数组的位置:
Collections.reverse(Arrays.asList(array));
It works since Arrays.asList returns a write-through proxy to the original array.
它工作以来数组。asList返回一个写入代理到原始数组。
#3
30
If you don't want to use Collections
then you can do this:
如果你不想使用集合,你可以这样做:
for (i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
#4
11
I like to keep the original array and return a copy. This is a generic version:
我喜欢保留原始数组并返回一个副本。这是一个通用版本:
public static <T> T[] reverse(T[] array) {
T[] copy = array.clone();
Collections.reverse(Arrays.asList(copy));
return copy;
}
without keeping the original array:
不保留原数组:
public static <T> void reverse(T[] array) {
Collections.reverse(Arrays.asList(array));
}
#5
9
try this:
试试这个:
public int[] reverse3(int[] nums) {
int[] reversed = new int[nums.length];
for (int i=0; i<nums.length; i++) {
reversed[i] = nums[nums.length - 1 - i];
}
return reversed;
}
My input was:
我输入的是:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
12 3 4 5 6 7 8 9 10 11 12
And the output I got:
我得到的输出是
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
12 11 10 9 8 7 6 5 4 3 2 1
#6
5
You could use org.apache.commons.lang.ArrayUtils : ArrayUtils.reverse(array)
你可以使用org.apache.commons.lang.ArrayUtils: ArrayUtils.reverse(array)
#7
4
In case you don't want to use a temporary variable, you can also do like this:
如果您不想使用临时变量,也可以这样做:
final int len = arr.length;
for (int i=0; i < (len/2); i++) {
arr[i] += arr[len - 1 - i]; // a = a+b
arr[len - 1 - i] = arr[i] - arr[len - 1 - i]; // b = a-b
arr[i] -= arr[len - 1 - i]; // a = a-b
}
#8
3
In place reversal with minimum amount of swaps.
用最少的掉期来代替。
for (int i = 0; i < a.length / 2; i++) {
int tmp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = tmp;
}
#9
2
Or you could loop through it backeards
或者你可以把它圈起来。
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i > 0; i--){
reversedArray[j++] = firstArray[i];
}
(note: I have not compiled this but hopefully it is correct)
(注意:我还没有编译它,但希望它是正确的)
#10
2
I would do something like this:
我会这样做:
public int[] reverse3(int[] nums) {
int[] numsReturn = new int[nums.length()];
int count = nums.length()-1;
for(int num : nums) {
numsReturn[count] = num;
count--;
}
return numsReturn;
}
#11
2
you messed up
你搞砸了
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i >= 0; i--){
reversedArray[j++] = firstArray[i];
}
#12
2
public void swap(int[] arr,int a,int b)
{
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public int[] reverseArray(int[] arr){
int size=arr.length-1;
for(int i=0;i<size;i++){
swap(arr,i,size--);
}
return arr;
}
#13
2
The following will reverse in place the array between indexes i
and j
(to reverse the whole array call reverse(a, 0, a.length - 1)
)
下面将在索引i和j之间反向放置数组(反向反转整个数组调用)。长度- 1))
public void reverse(int[] a, int i , int j) {
int ii = i;
int jj = j;
while (ii < jj) {
swap(ii, jj);
++ii;
--jj;
}
}
#14
-2
This code would help:
这段代码将帮助:
int [] a={1,2,3,4,5,6,7};
for(int i=a.length-1;i>=0;i--)
System.out.println(a[i]);
#15
-3
you can send the original array to a method for example:
您可以将原始数组发送到一个方法,例如:
after that you create a new array to hold the reversed elements
在此之后,您将创建一个新的数组来保存反转的元素。
public static void reverse(int[] a){
int[] reversedArray = new int[a.length];
for(int i = 0 ; i<a.length; i++){
reversedArray[i] = a[a.length -1 -i];
}
#1
69
Collections.reverse()
can do that job for you if you put your numbers in a List
of Integers
.
如果您将数字放在整数列表中,那么reverse()可以为您完成这项工作。
List<Integer> list = Arrays.asList(1, 4, 9, 16, 9, 7, 4, 9, 11);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
Output:
输出:
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 9, 4, 7, 9, 16, 9, 4, 1]
#2
59
If you want to reverse the array in-place:
如果你想反转数组的位置:
Collections.reverse(Arrays.asList(array));
It works since Arrays.asList returns a write-through proxy to the original array.
它工作以来数组。asList返回一个写入代理到原始数组。
#3
30
If you don't want to use Collections
then you can do this:
如果你不想使用集合,你可以这样做:
for (i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
#4
11
I like to keep the original array and return a copy. This is a generic version:
我喜欢保留原始数组并返回一个副本。这是一个通用版本:
public static <T> T[] reverse(T[] array) {
T[] copy = array.clone();
Collections.reverse(Arrays.asList(copy));
return copy;
}
without keeping the original array:
不保留原数组:
public static <T> void reverse(T[] array) {
Collections.reverse(Arrays.asList(array));
}
#5
9
try this:
试试这个:
public int[] reverse3(int[] nums) {
int[] reversed = new int[nums.length];
for (int i=0; i<nums.length; i++) {
reversed[i] = nums[nums.length - 1 - i];
}
return reversed;
}
My input was:
我输入的是:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
12 3 4 5 6 7 8 9 10 11 12
And the output I got:
我得到的输出是
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
12 11 10 9 8 7 6 5 4 3 2 1
#6
5
You could use org.apache.commons.lang.ArrayUtils : ArrayUtils.reverse(array)
你可以使用org.apache.commons.lang.ArrayUtils: ArrayUtils.reverse(array)
#7
4
In case you don't want to use a temporary variable, you can also do like this:
如果您不想使用临时变量,也可以这样做:
final int len = arr.length;
for (int i=0; i < (len/2); i++) {
arr[i] += arr[len - 1 - i]; // a = a+b
arr[len - 1 - i] = arr[i] - arr[len - 1 - i]; // b = a-b
arr[i] -= arr[len - 1 - i]; // a = a-b
}
#8
3
In place reversal with minimum amount of swaps.
用最少的掉期来代替。
for (int i = 0; i < a.length / 2; i++) {
int tmp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = tmp;
}
#9
2
Or you could loop through it backeards
或者你可以把它圈起来。
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i > 0; i--){
reversedArray[j++] = firstArray[i];
}
(note: I have not compiled this but hopefully it is correct)
(注意:我还没有编译它,但希望它是正确的)
#10
2
I would do something like this:
我会这样做:
public int[] reverse3(int[] nums) {
int[] numsReturn = new int[nums.length()];
int count = nums.length()-1;
for(int num : nums) {
numsReturn[count] = num;
count--;
}
return numsReturn;
}
#11
2
you messed up
你搞砸了
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i >= 0; i--){
reversedArray[j++] = firstArray[i];
}
#12
2
public void swap(int[] arr,int a,int b)
{
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public int[] reverseArray(int[] arr){
int size=arr.length-1;
for(int i=0;i<size;i++){
swap(arr,i,size--);
}
return arr;
}
#13
2
The following will reverse in place the array between indexes i
and j
(to reverse the whole array call reverse(a, 0, a.length - 1)
)
下面将在索引i和j之间反向放置数组(反向反转整个数组调用)。长度- 1))
public void reverse(int[] a, int i , int j) {
int ii = i;
int jj = j;
while (ii < jj) {
swap(ii, jj);
++ii;
--jj;
}
}
#14
-2
This code would help:
这段代码将帮助:
int [] a={1,2,3,4,5,6,7};
for(int i=a.length-1;i>=0;i--)
System.out.println(a[i]);
#15
-3
you can send the original array to a method for example:
您可以将原始数组发送到一个方法,例如:
after that you create a new array to hold the reversed elements
在此之后,您将创建一个新的数组来保存反转的元素。
public static void reverse(int[] a){
int[] reversedArray = new int[a.length];
for(int i = 0 ; i<a.length; i++){
reversedArray[i] = a[a.length -1 -i];
}