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.
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.
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.
The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With