Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between Array and ArrayList to get Prime numbers?

i was solving problem about the prime numbers, i found solution using Arrays, but i wanted to use the array list for some issues, one of them is to understand the Arraylist well. but i found that the results are not the same when i use the arraylist, here are the 2 Codes:

//using Arrays

import java.util.Arrays;
public class Prime {
    public static void main(String[] args) {
        primeNumber(100);
    }
    public static void primeNumber(int end) {
        boolean primeNumbers[] = new boolean[end]; 
        for (int i = 0; i < end; i++) {
            primeNumbers[i] = true;
        }
        for (int i = 2; i < primeNumbers.length; i++) {
            if (primeNumbers[i] ) {
                for (int j = i+i; j < primeNumbers.length; j += i) {
                    primeNumbers[j]= false;
                }
            }
        }
        for (int j = 2; j < primeNumbers.length; j++) {
            if (primeNumbers[j]) {
                System.out.println(j);

            }
        }   
    }
}

//Using ArrayList

import java.util.ArrayList;
public class Prime {
    public static void main(String[] args) {
        primeNumber(100);
    }

    public static void primeNumber(int end) {
        ArrayList<Boolean> primeNumbers = new ArrayList<Boolean>(); 
        for (int i = 0; i < end; i++) {
            primeNumbers.add(i,true);
        }
        for (int i = 2; i < primeNumbers.size(); i++) {
            if (primeNumbers.get(i) ) {
                for (int j = i+i; j < primeNumbers.size(); j += i) {
                    primeNumbers.add(j, false);
                }
            }
        }
        for (int j = 2; j < primeNumbers.size(); j++) {
            if (primeNumbers.get(j)) {
                System.out.println(j);

            }
        }

    }
}

Can i know what is the wrong i did with using the Arraylist, and if i wanted to use the arraylist for the same result what should i do.

Great thanks for the help.

like image 467
Ahmed Nassar Avatar asked Apr 27 '12 11:04

Ahmed Nassar


People also ask

Is ArrayList more efficient than array?

An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.

What are the advantages of ArrayList over arrays?

In short, ArrayList is more flexible than a plain native array because it's dynamic. It can grow itself when needed, which is not possible with the native array. ArrayList also allows you to remove elements which are not possible with native arrays.

What is the difference between ArrayList and array?

The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.

How do you find the sum of prime numbers in an array in Java?

Steps to Find the Sum of Prime NumbersRead or initialize the lower and upper limit. Iterate a loop (for or while) to find the prime numbers between the given range. If the number is prime, add that number to the variable sum and print the result.


2 Answers

Instead of

primeNumbers.add(j, false);

you should use

primeNumbers.set(j, false);

since add() adds a new element at the specified position (meaning the ArrayList grows by one element), but set() only sets the value of the element at specified position.

like image 142
Petr Janeček Avatar answered Sep 19 '22 02:09

Petr Janeček


primeNumbers.add(j, false);

adds the element to the array list, and shifting all elements with index >= j to the right [including the previous element j], while:

primeNumbers[j]= false

overrides the existing element j, and does not shift elements.

You are probably looking for ArrayList.set() instead of ArrayList.add()

like image 39
amit Avatar answered Sep 20 '22 02:09

amit