简体   繁体   中英

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). 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! 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; exactly what you are looking for. You can get its size by list1Array.size();

If you insist on having the final result as an array, then you can later call the toArray() method of ArrayList . This post will be helpful.

If you really want to use Arrays , do it like this:

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<Integer> list1Array = new ArrayList();

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

You should take the input as string and then use string.split(" "); and then obtain an array of Strings representing each number. Obtaining array by string can be done by string tokenizing.

UPDATE

But you should be careful not to put other chars as separators for numbers number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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