如何在 Java 中反转 int 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2137755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I reverse an int array in Java?
提问by MichaelScott
I am trying to reverse an int array in Java.
我正在尝试在 Java 中反转 int 数组。
This method does not reverse the array.
此方法不会反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
What is wrong with it?
它有什么问题?
采纳答案by 3lectrologos
To reverse an int array, you swap items up until you reach the midpoint, like this:
要反转 int 数组,您可以交换项目直到到达中点,如下所示:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
您这样做的方式是将每个元素交换两次,因此结果与初始列表相同。
回答by Tarik
public class ArrayHandle {
public static Object[] reverse(Object[] arr) {
List<Object> list = Arrays.asList(arr);
Collections.reverse(list);
return list.toArray();
}
}
回答by Nick Strupat
It is most efficient to simply iterate the array backwards.
简单地向后迭代数组是最有效的。
I'm not sure if Aaron's solution does this vi this call Collections.reverse(list);
Does anyone know?
我不确定 Aaron 的解决方案是否在此调用中执行此操作Collections.reverse(list);
有人知道吗?
回答by fastcodejava
Your program will work for only length = 0, 1
.
You can try :
您的程序仅适用于length = 0, 1
. 你可以试试 :
int i = 0, j = validData.length-1 ;
while(i < j)
{
swap(validData, i++, j--); // code for swap not shown, but easy enough
}
回答by Manur
With Commons.Lang, you could simply use
使用Commons.Lang,你可以简单地使用
ArrayUtils.reverse(int[] array)
Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.
大多数情况下,在处理您的问题时,坚持使用已经过单元测试和用户测试的易于获得的库会更快、更安全。
回答by Bill the Lizard
I think it's a little bit easier to follow the logic of the algorithm if you declare explicit variables to keep track of the indices that you're swapping at each iteration of the loop.
我认为如果您声明显式变量来跟踪您在循环的每次迭代中交换的索引,那么遵循算法的逻辑会更容易一些。
public static void reverse(int[] data) {
for (int left = 0, right = data.length - 1; left < right; left++, right--) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
}
}
I also think it's more readable to do this in a while loop.
我也认为在 while 循环中执行此操作更具可读性。
public static void reverse(int[] data) {
int left = 0;
int right = data.length - 1;
while( left < right ) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// move the left and right index pointers in toward the center
left++;
right--;
}
}
回答by AbsoluteBlue
If working with data that is more primitive (i.e. char, byte, int, etc) then you can do some fun XOR operations.
如果使用更原始的数据(即 char、byte、int 等),那么您可以执行一些有趣的 XOR 操作。
public static void reverseArray4(int[] array) {
int len = array.length;
for (int i = 0; i < len/2; i++) {
array[i] = array[i] ^ array[len - i - 1];
array[len - i - 1] = array[i] ^ array[len - i - 1];
array[i] = array[i] ^ array[len - i - 1];
}
}
回答by ghost programmer
public void display(){
String x[]=new String [5];
for(int i = 4 ; i > = 0 ; i-- ){//runs backwards
//i is the nums running backwards therefore its printing from
//highest element to the lowest(ie the back of the array to the front) as i decrements
System.out.println(x[i]);
}
}
回答by Kane
public static void main (String args[]){
//create array
String[] stuff ={"eggs","lasers","hats","pie","apples"};
//print out array
for(String x :stuff)
System.out.printf("%s ", x);
System.out.println();
//print out array in reverse order
for(int i=stuff.length-1; i >= 0; i--)
System.out.printf("%s ",stuff[i]);
}
回答by Krishna Kumar Chourasiya
This will help you
这会帮助你
int a[] = {1,2,3,4,5};
for (int k = 0; k < a.length/2; k++) {
int temp = a[k];
a[k] = a[a.length-(1+k)];
a[a.length-(1+k)] = temp;
}