简体   繁体   中英

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.

But let's say that I wouldn't know there are 25 prime numbers. 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 .

If in the end you absolutely need an array, you may create an array from your List .

List s are best for situations, when you don't know exact size of array. For example, you can use ArrayList .

You have to use List instead of an Array.

Compiler needs to know the size of an Array. When you define an Array like this. int[] primes = new int[0];

It creates space for only 1 integer. So, when you write to primes[1], it creates segmentation fault signal and your program crashes.

正如其他人所提到的,你可以使用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. for example

List Interface implemented by ArrayList and 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.

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()+" ");
    }

}

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