Java 如何在数组中存储数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29991342/
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
How to store numbers in an array?
提问by Hyman zhang
I just learned the array function in Java and now I wanted to store numbers from 1-19 in an array but don't know the right way to do it without the userinput function. Here is what I got, can you tell me if it is the right way to store number in array?
我刚刚在 Java 中学习了数组函数,现在我想将 1-19 的数字存储在数组中,但不知道没有 userinput 函数的正确方法。这是我得到的,你能告诉我这是否是在数组中存储数字的正确方法?
public class ArrayQuestion1 {
public static void main(String[] args) {
int a=0;
int array[] = new int [20];
for ( array[a]=1; array[a]<=19; array[a]++){
System.out.println(array[a]);
}
}
}
回答by QBrute
You would do something like this to fill your array with consecutive numbers from 0-19
你会做这样的事情来用 0-19 的连续数字填充你的数组
public class ArrayQuestion1 {
public static void main(String[] args) {
int array[] = new int [20];
for (int a = 0; a < array.length; a++){
array[a] = a;
}
}
}
回答by silentprogrammer
To store userinputs to int array you can do
要将用户输入存储到 int 数组,您可以执行
int array[] = new int [20];
Scanner scanner=new Scanner(System.in);
for ( i=0; i<array.length; i++){
array[i]=scanner.nextInt();
}
If you want to store number from 0 to 19 you can do
如果你想存储从 0 到 19 的数字,你可以这样做
int array[] = new int [20];
for ( i=0; i<array.length; i++){
array[i]=i;
}
回答by Hiren patel
If you don't want user input for array , you have to store numbers manually in the array like ,
如果您不希望用户输入数组,则必须手动将数字存储在数组中,例如,
int a=0;
int array[] = new int [20];
for ( a=1;a<=19; a++){
array[a]=a;
}
above code will store 0 to 19 in your array . than you can use below for loop to print it
上面的代码将在您的数组中存储 0 到 19。比你可以使用下面的循环来打印它
for ( a=1; a<=19; a++){
System.out.println(array[a]);
}
回答by Gabriele Mariotti
To use an array you have to declare it.
要使用数组,您必须声明它。
int array[] = new int [19];
If you want 19 numbers, then use an array with 19 elements.
如果您想要 19 个数字,则使用具有 19 个元素的数组。
Then you have to populate each number in the array. To obtain it, just use an index in your array:
然后你必须填充数组中的每个数字。要获得它,只需在数组中使用索引:
array[index] = value
For example:
例如:
for ( int i=0; i<array.lenght; i++){
array[i] = xx;
}
Pay attention. The first index in your array is 0 (not 1)
请注意。数组中的第一个索引是 0(不是 1)
回答by HenryDev
If you want to add consecutive numbers you can use a SIMPLE for loop and to see them on the screen you can just iterate your array. That is all. Hope this can help you!
如果你想添加连续的数字,你可以使用一个简单的 for 循环,并在屏幕上看到它们,你可以迭代你的数组。就这些。希望这可以帮到你!
class Main {
public static void main(String[] args) {
int a=0;
int array[] = new int [20];
for(int i = 0 ; i < array.length ; i++){
array[i] = i;
}
for(int x : array){
System.out.println(x);
}
}
}
回答by HenryDev
public static void main(String[] args) {
int array[] = new int[20];
for (int i = 1; i < array.length; i++){
array[i] = i;
}
//To print all the elements in the array.
for(int j=1; i< array.length; j++){
system.out.println(array[j]);
}
}
You can insert into the array using the above method and can view the contents of array also.
您可以使用上述方法插入数组,也可以查看数组的内容。