Java反向数组方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24518218/
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
Java reverse array method
提问by Diz
I'm trying to create a method that takes in an array and then returns that array in reverse. The code I wrote returns the array in reverse, but, the first two values are now 0. Anyone know what I did wrong?
我正在尝试创建一个接受数组然后反向返回该数组的方法。我写的代码反向返回数组,但是,前两个值现在是 0。有人知道我做错了什么吗?
public static int[] reverse(int[] x)
{
int []d = new int[x.length];
for (int i = 0; i < x.length/2; i++) // for loop, that checks each array slot
{
d[i] = x[i];
x[i] = x[x.length-1-i]; // creates a new array that is in reverse order of the original
x[x.length-1-i] = d[i];
}
return d; // returns the new reversed array
}
回答by Mureinik
You are assigning values from an uninitializedarray d
to x
- that's where the zeroes (default value for an int
in Java) are coming from.
您正在将未初始化数组中的值分配d
给x
- 这就是零(int
Java 中的默认值)的来源。
IIUC, you're mixing two reversing strategies.
IIUC,你混合了两种逆转策略。
If you're creating a new array, you needn't run over half of the original array, but over allof it:
如果您要创建一个新数组,则不需要运行超过原始数组的一半,而是运行所有数组:
public static int[] reverse(int[] x) {
int[] d = new int[x.length];
for (int i = 0; i < x.length; i++) {
d[i] = x[x.length - 1 -i];
}
return d;
}
Alternatively, if you want to reverse the array in place, you don't need a temp array, only a single variable (at most - there are also ways to switch two int
s without an additional variable, but that's a different question):
或者,如果您想就地反转数组,则不需要临时数组,只需要一个变量(最多 - 也有方法可以int
在没有附加变量的情况下切换两个s,但这是一个不同的问题):
public static int[] reverseInPlace(int[] x) {
int tmp;
for (int i = 0; i < x.length / 2; i++) {
tmp = x[i];
x[i] = x[x.length - 1 - i];
x[x.length - 1 - i] = tmp;
}
return x; // for completeness, not really necessary.
}
回答by user3657302
Its simple mistake; you are coping reversed data in x; and returning d. If you will return x, you will get complete revered data.
它的简单错误;您正在处理 x 中的反向数据;并返回 d. 如果您将返回 x,您将获得完整的受尊重数据。
d[i] = x[i]; // you are copying first element to some temp value
x[i] = x[x.length-1-i]; // copied last element to first; and respective...
x[x.length-1-i] = d[i]; // copied temp element to first element; and temp elements are nothing but array d
So ultimately you have created revered array inside x and not in d. If you will return x you got your answer. And d which is just half baked; so you get default value of 0 for remainign half array. :)
所以最终你在 x 中创建了受尊敬的数组,而不是在 d 中。如果您将返回 x,您就得到了答案。而 d 只是半熟;所以你得到剩余半阵列的默认值 0。:)
回答by Mohamad
Here is a short way to do it.
这是一个简短的方法。
public static int[] reverse(int[] x)
{
int[] d = new int[x.length]; //create new array
for (int i=x.length-1; i >= 0; i--) // revered loop
{
d[(x.length-i-1)]=x[i]; //setting values
}
return d; // returns the new reversed array
}