Java 如何创建一个空数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23067717/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:24:40  来源:igfitidea点击:

How to create an empty array?

javaarrays

提问by user3131148

Am new and I've searched the web and found it a bit frustrating, all I got was that in java array's can't be re-sized. I just want to figure out How do I create an empty array where I then prompt the user to enter numbers to fill the array? I don't want to define the capacity of the area like int[] myArray = new int[5];I want to define an empty array for which a user defines the capacity, example- they enter number after number and then enter the word end to end the program, then all the numbers they entered would be added to the empty array and the amount of numbers would be the capacity of the array.

我是新来的,我在网上搜索过,发现它有点令人沮丧,我得到的只是在 java 数组中无法重新调整大小。我只想弄清楚如何创建一个空数组,然后提示用户输入数字来填充数组?我不想定义区域的容量,就像int[] myArray = new int[5];我想定义一个用户定义容量的空数组一样,例如 - 他们在数字之后输入数字,然后输入单词 end 以结束程序,然后是所有数字他们输入的将被添加到空数组中,数字的数量将是数组的容量。

采纳答案by JNevens

You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:

您不能创建一个空数组,然后在用户在命令行中输入数字时让它动态增长。您应该读取数字并将它们放入 ArrayList 中。ArrayList 不需要初始大小并且会动态增长。像这样的东西:

public void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    BufferedReader console = 
            new BufferedReader(new InputStreamReader(System.in));
    while(true) {
        String word = console.readLine();
        if (word.equalsIgnoreCase("end") {
            break;
        } else {
            numbers.add(Integer.parseInt(word);
        }
    }

Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example

当然你不会使用while(true),你也不会把main它放进去,但这只是为了举个例子

回答by zaPlayer

回答by Rogue

How about:

怎么样:

Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int size = scan.nextInt();
int[] yourArray = new int[size];
//can even initialize it
Arrays.fill(yourArray, -1);

回答by iDJ2012i

Take nfrom the user input and:

n从用户输入:

int a [] = new int [n];

回答by user3533325

    import java.util.Scanner;


public class Ex {
public static void main(String args[]){
    System.out.println("Enter array size value");
    Scanner scanner = new Scanner(System.in);
    int size = scanner.nextInt();
    int[] myArray = new int[size];
    System.out.println("Enter array values");
    for(int i=0;i<myArray.length;i++){
        myArray[i]=scanner.nextInt();
    }
    System.out.println("Print array values");
    for(int i=0;i<myArray.length;i++){
        System.out.println("myArray"+"["+i+"]"+"="+myArray[i]);
    }
}
}