简体   繁体   English

在Java中为一系列整数创建数组?

[英]Create array for series of integers in Java?

I've been up for a few hours trying to find a solution. 我已经花了几个小时试图寻找解决方案。

My program asks the user to enter a list of integers (example: 5 2 5 6 6 1). 我的程序要求用户输入整数列表(例如:5 2 5 6 6 1)。 Then I would like to create an array and store each integer into its respective array index, consecutively. 然后我想创建一个数组并连续地将每个整数存储到它各自的数组索引中。

Here is the part of my program i'm having trouble with (this program was meant to perform calculations via a method later on, but I didn't include that): 这是我的程序的一部分我遇到了麻烦(这个程序的目的是通过以后的方法执行计算,但我没有包含它):

import java.util.Scanner;
public class Assignment627 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int x = 0;
    int[] list1Array = new int[1];

    System.out.println("Enter list1: ");
    while (input.hasNext()){
        list1Array[x] = input.nextInt();
        x++;
    }

As you can see, I am instantiating the array "list1Array" but the problem is I don't know how many integers the user would enter! 正如您所看到的,我正在实例化数组“list1Array”但问题是我不知道用户将输入多少个整数! If only there were a way of knowing how many integers have been input... Any help would be greatly appreciated! 如果只有一种方式可以知道输入了多少整数...任何帮助将不胜感激! Thanks, Sebastian 谢谢,塞巴斯蒂安

import java.util.Scanner;
public class Assignment627 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //int[] list1Array = new int[1];
        List<Integer> list1Array = new ArrayList<>();

        System.out.println("Enter list1: ");
        while (input.hasNext()){
            list1Array.add(input.nextInt());
        }   
    }

}

An ArrayList , is like an array that does not have a predefined size and it dynamically changes its size; ArrayList ,就像一个没有预定义大小的数组,它动态地改变它的大小; exactly what you are looking for. 正是你在寻找什么。 You can get its size by list1Array.size(); 你可以通过list1Array.size();得到它的大小list1Array.size();

If you insist on having the final result as an array, then you can later call the toArray() method of ArrayList . 如果您坚持将最终结果作为数组,那么稍后可以调用ArrayListtoArray()方法 This post will be helpful. 这篇文章会有所帮助。

If you really want to use Arrays , do it like this: 如果你真的想使用Arrays ,那就这样做:

import java.util.Scanner;

public class Assignment627 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int x = 0;
        int[] list1Array = new int[1];

        System.out.println("Enter list1: ");
        while (input.hasNext()) {
            list1Array[x] = input.nextInt();
            x++;
            int[] temp = new int[list1Array.length + 1];

            for (int i = 0; i < list1Array.length; i++) {
                temp[i] = list1Array[i];
            }

            list1Array = temp;
        }
    }
}
 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter elemnt size ");
        int size = input.nextInt();

        int x = 0;
        int[] list1Array = new int[size];

        for (int y = 0 ; y < size ; y++) {
            System.out.println("Enter number");
            list1Array[x] = input.nextInt();
            x++;
        }

        System.out.println(Arrays.toString(list1Array));
    }

Output 产量

Enter elemnt size 
4
Enter number
2
Enter number
3
Enter number
4
Enter number
2
[2, 3, 4, 2]

Use List , in your case ArrayList will be fine: 使用List ,在你的情况下ArrayList会很好:

List<Integer> list1Array = new ArrayList();

while (input.hasNext()){
    list1Array.add(input.nextInt());
    x++;
}

You should take the input as string and then use string.split(" "); 您应该将输入作为字符串,然后使用string.split(“”); and then obtain an array of Strings representing each number. 然后获取表示每个数字的字符串数组。 Obtaining array by string can be done by string tokenizing. 按字符串获取数组可以通过字符串标记来完成。

UPDATE UPDATE

But you should be careful not to put other chars as separators for numbers number 但是你应该注意不要将其他字符作为数字的分隔符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM