# ArrayList, Array, and Method in "Prime Directive" exercise: Code works but not understanding the method that uses all 3!

My question relates the one method in the PRIME DIRECTIVE exercise that uses all 3 of these. I understand what the program is supposed to do (one method to determine if a number is prime, and the other to store only prime numbers from an array. I get all that.

I also understand how arrays and arrraylists are different. But for some reason my mind can’t wrap around what is happening with these 2 lines.

public ArrayList onlyPrimes(int[] numbers) {
ArrayList primes = new ArrayList();

it’s like merging an arrayList with a method and using an array as a parameter. Can someone break down what is happening here in a very simple example/explanation? I cant understand how it’s all connected.

I’ve done some searches but no one has asked this so far. Here is the whole code for those not familiar with the exercise.
Any help by the pros on here is appreciated!

import java.util.ArrayList;
class PrimeDirective {

public boolean isPrime(int number) {
if (number==2) {
return true;
} else if (number<2) {
return false;
}
for(int i=2;i<number;i++) {
if(number % i==0) {
return false;
}
}
return true;
}

public ArrayList onlyPrimes(int[] numbers) {
ArrayList primes = new ArrayList();

for (int number : numbers) {
if (isPrime(number)) {
}
}
return primes;
}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();
int numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

System.out.println(pd.isPrime(7));
System.out.println(pd.isPrime(28));
System.out.println(pd.isPrime(2));
System.out.println(pd.isPrime(0));

System.out.println(pd.onlyPrimes(numbers));

}

}

onlyPrimes is the name of the method. It is a public method and it has a single parameter named numbers. numbers is expected to be an Array of Integers. Once the statements inside the body of the method (the curly braces {} designating the body of the method) are executed, the method is expected to return a value. Since the signature specifies ArrayList as the data type of the return value, so the method must return an ArrayList. We don’t care what variable name is used within the body of the method to work with this ArrayList. All that we care is that when the method returns any value, the returned value must have a data type of ArrayList. If we don’t return an ArrayList or return a value of some other data type, we will get an error.

If the method isn’t expected to return any value, the signature will show void as the return type, e.g.

public void onlyPrimes(int[] numbers) { ...

If the method is expected to return an integer, the signature will show int as the return type, e.g.

public int onlyPrimes(int[] numbers) { ...

In the code you posted, the onlyPrimes method expects an Array of Integers to be passed as the argument. Within the body of the method, we are going to traverse the elements of this Array and decide whether each element is prime or not. Since, we don’t know beforehand how many elements will pass/fail the test, so we want to store the prime numbers in a dynamic list (i.e. in an ArrayList) instead of a fixed-size Array.
Within the body of the method,

ArrayList primes = new ArrayList();

we are declaring a variable named primes whose data type is ArrayList. We have initialized primes by creating a new instance of the ArrayList Class i.e.    new ArrayList()     by invoking the constructor of the ArrayList class. This will create an empty ArrayList. When we iterate over the numbers array and some element passes the test for primes, then we will add it to the primes ArrayList. When we have processed all the elements of the numbers array, we return the primes ArrayList from the method.

I can’t thank you enough for this explanation. I know it took time. Many thanks. From this, i discovered what had confused me. I know that arrayList is a class and has special methods it can use. This is the first time, however, i have seen it used as a return type since up until now it’s just been int, String, etc…

You also explained that it doesnt matter what the name of the variable we use for the new arrayList in the body - that is has no connection to the arrayList listed as return type. Now this makes sense too.

Finally, this exercise was also the first time seeing an int array used as a parameter. Have seen only primitives or Strings until now. That confused me as well.

Again, thank you again and I am very grateful. I guess i need to learn more about objects and classes now.