Java for 循环以反向打印整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21266547/
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
for loop to print the integer array in reverse
提问by user3220565
I am writing a program for creating a for loop to print the integer array in reverse order.
我正在编写一个程序来创建一个 for 循环来以相反的顺序打印整数数组。
public class RevIntArray {
public static void main(String[] args) {
int var[]=new int[3];
int i;
for(i = -3; i > -3; i++) {
System.out.println(i);
}
}
}
I am not receiving any results. Can someone provide a simple solution to this?
我没有收到任何结果。有人可以为此提供一个简单的解决方案吗?
回答by T McKeown
arrays have no concept of negative indices, if you want to traverse from the last to first then you must START at the LAST index and move to the FIRST.
数组没有负索引的概念,如果你想从最后一个到第一个遍历,那么你必须从最后一个索引开始并移动到第一个。
for(i =3; i>0; i--){
System.out.println(var[i - 1]);
}
回答by takendarkk
You were starting with a negative index which does not exist in arrays. It's also good practice to use array.length
instead of an explicit number in case you change the size of the array later on.
您从数组中不存在的负索引开始。array.length
如果您稍后更改数组的大小,使用而不是显式数字也是一种很好的做法。
public class RevIntArray {
public static void main(String[] args) {
int var[] = new int[] {1,2,3};
for(int i = var.length - 1; i >= 0 ; i--) {
System.out.println(var[i]);
}
}
}
回答by Fritz
A couple of hints to get you going:
几个提示让你继续前进:
for(i =-3; i>-3; i++){
System.out.println(i);
}
- The values in
var
, beingint
, will be all zeroes (due to the default initialization of theint
primitive type). Give your array some values so you can effectively check that you're printing it in reverse (for examplevar[2] = 1
). Otherwise, make it explicit with{0,0,1}
). - Your iteration is wrong. If the size is three, don't start from -3. Even if you got the loop to work, negative indexes are invalid for an array.
- The condition isn't met to begin with, so your
for
is pointless as it is. - Think about what you're trying to do: Iterating "the other way around" means starting from the endand going back to the beginning. You want to start in the last element and go back to index
0
. - Once you're done with the loop, printing
i
will show the index, not the element. You want the value stored invar
for that index. i--
will decrease your counter.var.length
will tell you the size of the array. Remember the indexes start on zero, solength - 1
is the index of the last element.- To obtain the value stored in a certain position of an array do
array[index]
.
- 中的值
var
,作为int
将是全零(由于缺省初始化int
原语类型)。给你的数组一些值,这样你就可以有效地检查你是否在反向打印它(例如var[2] = 1
)。否则,用{0,0,1}
)明确表示。 - 你的迭代是错误的。如果大小为 3,则不要从 -3 开始。即使您让循环工作,负索引对于数组也是无效的。
- 条件一开始就没有满足,所以你
for
是没有意义的。 - 想想你要做什么:“反过来”迭代意味着从最后开始,然后再回到开始。您想从最后一个元素开始并返回到 index
0
。 - 完成循环后,打印
i
将显示索引,而不是元素。您希望var
为该索引存储值。 i--
会减少你的计数器。var.length
会告诉你数组的大小。记住索引从零开始,length - 1
最后一个元素的索引也是如此。- 要获取存储在数组某个位置的值,请执行以下操作
array[index]
。
With that in mind, try again.
考虑到这一点,再试一次。
回答by bblincoe
First off, you should provide some values to your array:
首先,您应该为数组提供一些值:
int[] var = { 1, 2, 3 };
int[] var = { 1, 2, 3 };
Then, you should initialize your loop counter. You can do this inside the loop structure. Now for the loop:
然后,您应该初始化您的循环计数器。您可以在循环结构内执行此操作。现在循环:
for (int i = var.length - 1; i >= 0; --i)
{
System.out.println(var[i]);
}
This should print 321to the console.
这应该将321打印到控制台。