简体   繁体   English

在没有定义大小的情况下声明int []数组?

[英]Declaring int[] Array without defining size?

This is my code: 这是我的代码:

int[] primes = new int[25];

    for (int i = 2; i <= 100; i++)
    {
        for (int j = i; j > 0; j--)
        {
            int prime = i%j;
            if (prime == 0)
            {
                if ((j == i) || (j == 1))
                {
                    isPrime = true;
                }
                else
                {
                    isPrime = false;
                    break;
                }
            }
        }

        if (isPrime == true){
            primes[index] = i;
            index++;
        }
    }

As you can see, I'm writing prime numbers into array. 如您所见,我正在将素数写入数组。 I counted all the prime numbers that are within range 2-100, so I set array size to 25. 我计算了2-100范围内的所有素数,所以我将数组大小设置为25。

But let's say that I wouldn't know there are 25 prime numbers. 但是,让我们说我不知道​​有25个素数。 Is there a way to create an array (any type of array) without defining the size? 有没有办法在不定义大小的情况下创建数组(任何类型的数组)? I tried this: 我试过这个:

int[] primes = new int[0];

But it crashed. 但它崩溃了。

您可以将List用于未知大小的数据(可能ArrayList将是您的最佳解决方案)。

No; 没有; an array's size must be declared ahead of time. 必须提前声明数组的大小。 Try taking a look at the different implementations of List or Vector . 试着看看ListVector的不同实现。

If in the end you absolutely need an array, you may create an array from your List . 如果最终你绝对需要一个数组,你可以从List创建一个数组。

List s are best for situations, when you don't know exact size of array. 当您不知道阵列的确切大小时, List最适合情况。 For example, you can use ArrayList . 例如,您可以使用ArrayList

You have to use List instead of an Array. 您必须使用List而不是Array。

Compiler needs to know the size of an Array. 编译器需要知道数组的大小。 When you define an Array like this. 定义像这样的数组时。 int[] primes = new int[0]; int [] primes = new int [0];

It creates space for only 1 integer. 它只为1个整数创建空间。 So, when you write to primes[1], it creates segmentation fault signal and your program crashes. 因此,当您写入primes [1]时,它会创建分段故障信号并导致程序崩溃。

正如其他人所提到的,你可以使用ArrayList ,如果仍然需要输出作为数组,你可以在最后将ArrayList转换为数组(一旦知道了数组的大小)。

Arrays are meant for the situation when we know about the size and number of elements we are going to put inside.If we are not sure about the size then in that case Collection API of Java can help you out. 当我们知道要放入的元素的大小和数量时,数组就是针对这种情况的。如果我们不确定大小,那么在这种情况下,Java的Collection API可以帮助你。 for example 例如

List Interface implemented by ArrayList and Vector 列表接口由ArrayList和Vector实现

ArrayList can extends its size 50% at runtime so this will solve your problem.Or else you can use Vector if your application dealing with thread safety.but avoid using Vector because It increases its size to double at runtime. ArrayList可以在运行时将其大小扩展50%,这样就可以解决您的问题。如果您的应用程序处理线程安全,则可以使用Vector。但是避免使用Vector,因为它在运行时将其大小增加到了两倍。

This code may help you. 此代码可能对您有所帮助。

public static void main(String[] args) {

    boolean isPrime;
    List<Integer> list = new ArrayList<Integer>();

    for (int i = 2; i <= 100; i++)
    {
        isPrime = true;
        for (int j = 2; j < i/2; j++)
        {
            int prime = i % j;
            if (prime == 0)
            {
                isPrime = false;
                break;
            }
        }

        if (isPrime == true){
            list.add(i);
        }
    }

    // all the prime numbers 
    for(Iterator<Integer> iterator = list.iterator(); iterator.hasNext();){
        System.out.print(iterator.next()+" ");
    }

}

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

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