4

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.

2
  • an array has a fixed size, by definition. Use a List if your need to expand it. Commented Dec 24, 2013 at 15:42
  • int[0] is an empty array, so of course it would crash. Commented Dec 24, 2013 at 15:42

7 Answers 7

12

You can use List for unknown size of data (probably ArrayList will be the best solution for you).

Sign up to request clarification or add additional context in comments.

1 Comment

More specifically, an ArrayList has similar behavior to an array, but grows as needed.
1

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.

Comments

0

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

Comments

0

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.

Comments

0

As mentioned by others, you can use an ArrayList, and if you still need the output as an array, you can convert the ArrayList to array at the end (once you know the size of the array).

Comments

0

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.

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.