简体   繁体   English

有没有办法在 Java 中创建一个空数组,但可以有无限量

[英]Is there a way to create an array in Java that is empty, but can have an indefinite amount

I'm working on a program in java that takes all user inputted numbers until -99 with a loop, I am using Arrays but I have no way of knowing how many numbers will be inputted.我正在 java 中开发一个程序,该程序将所有用户输入的数字带入 -99 循环,我正在使用 Arrays 但我无法知道将输入多少个数字。 I'm also a novice with java and I don't know that much.我也是 java 的新手,我不太了解。 ` import java.util.*; ` 进口 java.util.*; public class ArrayComputing {公共 class ArrayComputing {

public  int[] makeArray()
{
    int[] nums = //where I need help
    int numArray;
    int i =0;
    Scanner input = new Scanner(System.in);
    boolean negative99=false;
    while (negative99==false)
    {
    i++;
    System.out.println("Enter a number");
    numArray= input.nextInt();
    if (numArray==-99)
    {
        break;
    }
    else    
    {
    nums[i]=numArray;
    System.out.println(Arrays.toString(nums));
    }
    
    }
    return nums;
    
    
    
}

} ` } `

If you use List (ArrayList in particular) it will do what you need.如果您使用 List(特别是 ArrayList),它将满足您的需求。 It can expand as you add the members to the list and will not run out space.它可以在您将成员添加到列表时扩展,并且不会用完空间。 As opposed to Array where you need to allocate space in advance and need to know how many members to expect.与 Array 不同,您需要提前分配空间并需要知道预期有多少成员。

What you're asking for, is a behaviour of Dynamic Array Abstract Data Type, one popular implementation of which, is the ArrayList class in Java .您要的是Dynamic Array抽象数据类型的一种行为,其中一个流行的实现是 Java 中的ArrayList class

In the case of raw/built-in new int[]{..} array, Java Specification (and mostly all other languages) require you to allocate a specific segment of the memory, for which, you need to provide a size of the array.对于原始/内置new int[]{..}数组, Java Specification (以及大多数其他语言)要求您分配 memory 的特定段,为此,您需要提供大批。

In your case, ArrayList , or LinkedList will help.在您的情况下, ArrayListLinkedList会有所帮助。

An array is a structure of defined size that can, to my knowledge, not directly be extended or reduced in size.数组是一种定义大小的结构,据我所知,它不能直接扩展或减小大小。

You may however use an ArrayList, which is of undefined size.但是,您可以使用大小未定义的 ArrayList。

ArrayList<Integer> numberList = new ArrayList();

You can then "add" values to it:然后,您可以向其中“添加”值:

numberList.add(value);

The usage of ArrayList may be found here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html The usage of ArrayList may be found here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

You may do so until your computer runs out of ressources to store the values that are contained in the ArrayList.您可以这样做,直到您的计算机用完存储 ArrayList 中包含的值的资源。

Unfortunately the length of an array can't be negative.不幸的是,数组的长度不能为负。 It will throw a NegativeArraySizeException.它将抛出 NegativeArraySizeException。

Anyway you can first let the user define a length and afterwards you can create one with this size.无论如何,您可以先让用户定义一个长度,然后您可以创建一个具有此大小的长度。

    public static  int[] makeArray() {
    
    Scanner input = new Scanner(System.in);
    
    System.out.print("> ");
    int in = input.nextInt();
    int[] nums = new int[in];
    
    int numArray;
    int i =0;
    //...

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

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