Declaring int[] Array without defining size?

Viewed 41016

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.

7 Answers
Related