英文:
Why is my array showing 0s when I flip it and reverse it?
问题
尝试反转用户输入的数组。
我的代码
Scanner s = new Scanner(System.in);
System.out.println("输入数字:");
int[] array = new int[5];
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
array[i] = s.nextInt();
reversed[i] = array[array.length - 1 - i];
}
System.out.println(Arrays.toString(reversed));
当我输入:
10
20
30
40
50
我得到:
输出:
[0, 0, 30, 20, 10]
然而,当我硬编码数字时,它正常工作。
int[] array = {10, 20, 30, 40, 50};
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
reversed[i] = array[array.length - 1 - i];
}
System.out.println(Arrays.toString(reversed));
我得到:
[50, 40, 30, 20, 10]
不确定我在这里做错了什么。我知道有其他方法可以做同样的事情,但我正在尝试弄清楚为什么我这样做会出现问题。
英文:
Trying to reverse an user-input array.
My code
Scanner s = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[5];
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
array[i] = s.nextInt();
reversed[i] = array[array.length - 1 - i];
}
System.out.println(Arrays.toString(reversed));
When I enter:
10
20
30
40
50
I get:
output:
[0, 0, 30, 20, 10]
Yet when I hardcode numbers, it works just fine.
int[] array = {10, 20, 30, 40, 50};
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
reversed[i] = array[array.length - 1 - i];
}
System.out.println(Arrays.toString(reversed));
I get back:
[50, 40, 30, 20, 10]
Not sure what I'm doing wrong here. I know there are other ways to do the same thing but I'm trying to figure out why the way I'm doing it is giving me issues.
答案1
得分: 2
你正在从开头到结尾填充数组,并且在填充 reversed
之前,你正在使用尚未填充的 array
中的值!
具体来说,一旦你尝试填充 reversed[0]
,你使用的是尚未填充的 array[4]
。
你可以通过先填充 array
,然后在第一个循环完成后再开始用第二个循环填充 reversed
来克服这个问题。
英文:
You are filling your arrays from beginning to end, and you are filling reversed
with values from array
, before they were populated!
Specifically, once you try to fill reversed[0]
, you use array[4]
, which was not populated yet.
You can overcome it, by first filling array
, and start filling reversed
in a second loop after the first is complete.
答案2
得分: 2
将这一行代码更改为:
reversed[array.length-1-i] = array[i];
如注明的那样,您正在尝试在数组填充之前访问它。
英文:
just change this line:
reversed[i] = array[array.length - 1 - i];
to:
reversed[array.length-1-i] = array[i];
as noted you are trying to access array before it is filled
答案3
得分: 0
尝试使用以下代码来实现数组的反转:
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[5];
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
array[i] = s.nextInt();
}
int j = array.length;
for (int i = 0; i < array.length; i++) {
reversed[j - 1] = array[i];
j = j - 1;
}
System.out.println("Reversed array is: \n");
for (int k = 0; k < array.length; k++) {
System.out.println(reversed[k]);
}
}
}
输出:
Enter numbers:
1
2
3
4
5
Reversed array is:
5
4
3
2
1
英文:
Try the below code to implement the reversal of the array :
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[5];
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
array[i] = s.nextInt();
}
int j = array.length;
for (int i = 0; i < array.length; i++) {
reversed[j - 1] = array[i];
j = j - 1;
}
System.out.println("Reversed array is: \n");
for (int k = 0; k < array.length; k++) {
System.out.println(reversed[k]);
}
}
}
Output :
Enter numbers:
1
2
3
4
5
Reversed array is:
5
4
3
2
1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论