java 反转数组的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3843483/
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
Reversing elements of an array
提问by Snowman
Given an array a and two other int variables, k and temp, write a loop that reverses the elements of the array.
给定一个数组 a 和另外两个 int 变量 k 和 temp,编写一个循环来反转数组的元素。
for (k = 0; k < a.length-1; k++) {
temp = a[k];
a[k] = a[a.length-1-k];
a[a.length-1-k] = temp;
}
This is not working. Any idea why?
这是行不通的。知道为什么吗?
回答by Nikita Rybak
E.g., for a = [0, 1, 2, 3, 4, 5]
you'll switch 0
and 5
twice: when i == 0
and when i == 5
. This double-switching will put both elements into their original positions: (0, 5) -> (5, 0) -> (0, 5)
.
例如,因为a = [0, 1, 2, 3, 4, 5]
您将切换0
和5
两次: wheni == 0
和 when i == 5
。这种双重切换会将两个元素置于其原始位置:(0, 5) -> (5, 0) -> (0, 5)
。
Try to make your loop to go through half of array only: so each pair is switched once.
试着让你的循环只通过一半的数组:所以每一对都切换一次。
回答by JoshD
You need to stop your loop at a.length/2 (in the middle).
您需要在 a.length/2(中间)处停止循环。
for(k=0;k<a.length/2;k++)
This should work for odd and even length arrays if this is integer division.
如果这是整数除法,这应该适用于奇数和偶数长度的数组。
回答by steve
check this at my blog hope this going to help http://codeheaven.wordpress.com/
在我的博客上查看这个希望这会有所帮助http://codeheaven.wordpress.com/
Here is the code from above link:
这是上面链接中的代码:
public class ArrayReversing {
public static void main(String a[]){
int arr[]={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int temp;
int as = arr.length;
int k = as – 1;
System.out.println(“Array Before Reversing”);
printArray(arr);//method used to print array on screen
ArrayReverse://using loops with title
for(int i = 0; i < arr.length/2 ; i++){
temp = arr[k];// swaping
arr[k] = arr[i];
arr[i] = temp;
k–;
}
System.out.println(“Array After Reversing”);
printArray(arr); // calling the method printArray to print the elements of array
}
static void printArray(int ar[]){
PrintArray:
for(int l:ar)
System.out.println(l);
}
}
Output:
输出:
Array Before Reversing
1
2
3
4
5
6
7
8
Array After Reversing
8
7
6
5
4
3
2
1
回答by Evan Henry
Use a Stack. A stack reverses the elements that are added to it. A stack can be described as First In, Last Out (FILO). "Push" adds the elements to the stack and "Pop" removes them.
使用堆栈。堆栈反转添加到它的元素。堆栈可以描述为先进后出 (FILO)。“推送”将元素添加到堆栈中,“弹出”删除它们。
public static int[] num1 = {1,2,3,4,5,6};
public static Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) {
for(int i = 0; i < num1.length; i++){
stack.push(num1[i]);
}
for(int i = 0; i < num1.length; i++){
System.out.print(stack.pop());
}
}
Output:
输出:
654321
回答by Bablu kumar
Try this will simply work:
试试这将简单地工作:
public class ReverseArray{
public static void main(String[] args){
int[] a ={1,2,3,4,5};
for(int i=a.length-1;i>=0;i--){
System.out.print(a[i]);
}
}
}
回答by codaddict
You might also want to look at the ArrayUtils.reversemethod.
您可能还想查看ArrayUtils.reverse方法。
Hereis an example using that method.
这是使用该方法的示例。
I know you cannot use it in this assignment. But you should be aware of this and use it whenever possible, say in your assignments, projects.
我知道你不能在这个作业中使用它。但是你应该意识到这一点并尽可能使用它,比如在你的作业、项目中。
回答by Krishna Kumar Chourasiya
This will work
这将工作
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;
}
回答by Noel M
You are swapping elements from each end of the array... and iterating to the items you've already swapped... is that enough of a hint?
您正在从数组的每一端交换元素......并迭代到你已经交换过的项目......这是否足够提示?