java 为了交换这个数组中的数字,我缺少什么?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39813158/
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
What am I missing in order to swap numbers in this array? Java
提问by Admin Lost
So in my assignment, I'm halfway of solving this particular java array (this is an online book; percentage is at 50% in testing phase). However I cannot figure out what I'm missing. For this code, I have to make the input reverse two numbers (of whatever amount is requested). I tried looking online for similar situation but none answered what I needed.
因此,在我的作业中,我正在解决这个特定的 java 数组(这是一本在线书籍;测试阶段的百分比为 50%)。但是我无法弄清楚我错过了什么。对于此代码,我必须使输入反转两个数字(无论请求的数量如何)。我尝试在网上寻找类似的情况,但没有人回答我的需要。
Directions:Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4.
说明:编写一个方法 swapArrayEnds() 来交换其数组参数的第一个和最后一个元素。例如:sortArray = {10, 20, 30, 40} 变成 {40, 20, 30, 10}。数组的大小可能不同于 4。
import java.util.Scanner;
public class ModifyArray {
// ANYTHING ABOVE THIS COMMENT CANNOT BE MODIFIED
public static void swapArrayEnds(int[] sortArray, int numElem) {
numElem = 4;
int i = 0; // Loop index
int tmpStore = 0; // Temp variable for swapping
for (i = 0; i < numElem; ++i) {
tmpStore = sortArray[i]; // Do swap
sortArray[i] = sortArray[numElem - 1];
sortArray[numElem - 1] = tmpStore;
}
return;
}
// ANYTHING BELOW THIS COMMENT CANNOT BE MODIFIED
public static void main (String [] args) {
int numElem = 4;
int[] sortArray = new int[numElem];
int i = 0;
sortArray[0] = 10;
sortArray[1] = 20;
sortArray[2] = 30;
sortArray[3] = 40;
swapArrayEnds(sortArray, numElem);
for (i = 0; i < numElem; ++i) {
System.out.print(sortArray[i]);
System.out.print(" ");
}
System.out.println("");
return;
}
}
At the moment, my error code is:
目前,我的错误代码是:
Testing with original sortArray = {10, 20, 30, 40}
Expected output: 40 20 30 10
Your output: 40 10 20 30
Tests aborted.
I'm not sure how my input became like that, but it merely placed the original last number in front of the original first. How do I have it that the first and last numbers are swapped?
我不确定我的输入是如何变成那样的,但它只是将原始最后一个数字放在原始数字前面。我如何让第一个和最后一个数字交换?
回答by Adrian Colomitchi
Since onlythe ends need to be swapped, and this swap need to happen exactly once(not repeatedly), how about:
由于只需要交换两端,并且这个交换只需要发生一次(不是重复),怎么样:
// Why do you need yo swap it in a cycle, thus many times???
/* Commenting faulty code out
for (i = 0; i < numElem; ++i) {
tmpStore = sortArray[i]; // Do swap
sortArray[i] = sortArray[numElem - 1];
sortArray[numElem - 1] = tmpStore;
}
*/
// A single time should do
tmpStore = sortArray[0]; // Do swap with the starting end
sortArray[0] = sortArray[numElem - 1];
sortArray[numElem - 1] = tmpStore;
(note: I think I'm getting tired of inane comments/answers sending a beginner on fool's errands)
(注意:我想我已经厌倦了向初学者发送傻瓜差事的愚蠢评论/答案)
回答by Lubrious
public static void swapArrayEnds(int sortArray[],int numElem ) {
int t;
t = sortArray[numElem-1];
//assigning last element to temporary variable
sortArray[numElem-1]=sortArray[0];
//swapping last element with first element
sortArray[0]=t;
}
回答by liducktoe
//a fix the the first answer provided
import java.util.Scanner;
public class ModifyArray {
// ANYTHING ABOVE THIS COMMENT CANNOT BE MODIFIED
for (int i = 0; i < numElem; i++) {
int temp = sortArray[i];
sortArray[i] = sortArray[numElem - 1];
sortArray[numElem - 1] = temp;
return;
}
}
// ANYTHING BELOW THIS COMMENT CANNOT BE MODIFIED
public static void main (String [] args) {
int numElem = 4;
int[] sortArray = new int[numElem];
int i = 0;
sortArray[0] = 10;
sortArray[1] = 20;
sortArray[2] = 30;
sortArray[3] = 40;
swapArrayEnds(sortArray, numElem);
for (i = 0; i < numElem; ++i) {
System.out.print(sortArray[i]);
System.out.print(" ");
}
System.out.println("");
return;
}
}
回答by Roadrunner
public static void swapArrayEnds(int sortArray [],int numElem) {
int temp = sortArray[0];
sortArray[0] = sortArray[numElem - 1];
sortArray[numElem - 1] = temp;
}
回答by mnbv
You may try this approach outlined below.
您可以尝试下面概述的这种方法。
public static void swapArrayEnds(int[] newArray){
int num = newArray[0] ;
newArray[0] = newArray[newArray.length-1];
newArray[newArray.length-1] = num;
}