java 复制和修改数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36001310/
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
Copying and modifying array elements
提问by java2019
Here is the original prompt:
这是原始提示:
Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.
Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element array (newScores = {10, 20, 30, 40}), the second with a 1-element array (newScores = {199}). See How to Use zyBooks.
Also note: If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message.
编写一个循环,将 newScores 设置为向左移动一次的 oldScores,并将元素 0 复制到末尾。例如:如果 oldScores = {10, 20, 30, 40},则 newScores = {20, 30, 40, 10}。
注意:这些活动可能会使用不同的测试值来测试代码。此活动将执行两个测试,第一个使用 4 元素数组 (newScores = {10, 20, 30, 40}),第二个使用 1 个元素数组 (newScores = {199})。请参阅如何使用 zyBooks。
另请注意:如果提交的代码尝试访问无效的数组元素,例如 4 元素数组的 newScores[9],则测试可能会产生奇怪的结果。或者测试可能会崩溃并报告“程序结束从未到达”,在这种情况下,系统不会打印导致报告消息的测试用例。
This is the code that I have:
这是我拥有的代码:
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i = 0;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
for (i = 0; i < SCORES_SIZE - 1; i++) {
newScores[3] = oldScores[0];
newScores[i] = oldScores[i + 1];
}
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(newScores[i] + " ");
}
System.out.println();
return;
}
}
This is my output:
这是我的输出:
Testing for oldScores = {10, 20, 30, 40}
测试 oldScores = {10, 20, 30, 40}
Your output: 20 30 40 10
你的输出:20 30 40 10
? Testing for oldScores = {199}
? 测试 oldScores = {199}
Expected output: 199
预期输出:199
Your output: 0
您的输出:0
Why am I getting 0 for my second output test?
为什么我的第二次输出测试得到 0?
回答by Andy Turner
The for loop in which you copy the values from oldscores
to newscores
is never run in the case of SCORES_SIZE == 1
, since SCORES_SIZE - 1 == 0
, and 0 < 0
is false immediately.
从oldscores
to复制值的 for 循环newscores
永远不会在SCORES_SIZE == 1
, since的情况下运行SCORES_SIZE - 1 == 0
,并且0 < 0
立即为假。
Move the newScores[SCORES_SIZE - 1] = oldScores[0];
line outside the for loop:
将newScores[SCORES_SIZE - 1] = oldScores[0];
行移到 for 循环之外:
for (i = 0; i < SCORES_SIZE - 1; i++) {
newScores[i] = oldScores[i + 1];
}
newScores[SCORES_SIZE - 1] = oldScores[0];
回答by Bobby C. Robillard
Assuming you the only other check is an array with length one, simply use this
假设您唯一的其他检查是一个长度为 1 的数组,只需使用它
if(oldscores.length==1)
{newscores[0]=oldscores[0];
System.out.println(newscores[0]);
}
or even more simple,
或者更简单,
if(oldscores.length==1)
{
System.out.println(oldscores[0]);
}