Click here to Skip to main content
14,878,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is simple, I need to remove all primes from an arraylist(called al) and print it.

I created a second arraylist(called noPrime) to store all the non-primes, and then stored the noPrime's reference in al and printed it twice. Once inside the solution() method and once in the main method AFTER solution() was already called.


Yet,

When I print the array al in the solution method, it outputs the correct answer.

When I print it in the main method, after calling the solution method, it outputs the incorrect answer.

Example:
Original = [3, 12, 13, 15]
first printing = [12, 15]
second printing = [3, 12, 13, 15]

I suspect it is because noPrime has been declared within solution() and it gets destroyed once the solution() has returned. So al, which was holding the reference of noPrime, also gets destroyed? And it comes back to holding its original reference?

Why is this happening?

what does al = noPrime really do?

What I have tried:

Java
import java.io.*;
import java.util.*;

public class Main {

    private static boolean isPrime(int num)
    {
        int rootnum = (int)Math.sqrt(num);
        
        for(int i = 2; i <= rootnum; i++ )
        {
            if(num%i == 0)
                return false;
            
        }
        
        return true;
    }
	public static void solution(ArrayList<Integer> al){
	    ArrayList<Integer> noPrime = new ArrayList<>();
	    int size = al.size();
	    
	    //System.out.println(isPrime(19));
        for(int i = 0; i < size ; i++)
        {
            int num = al.get(i);
            if(!isPrime(num))
                noPrime.add(num);
        }
        
        al = noPrime;
        System.out.println(al);
    }
	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		ArrayList<Integer> al = new ArrayList<>();
		for(int i = 0 ; i < n; i++){
			al.add(scn.nextInt());
		}
		System.out.println("Original = " + al);
		solution(al);
		System.out.println(al);
	}

}
Posted
Updated 1-Apr-21 1:30am
v2

1 solution

In Java methods, arguments are passed by value, so the
Java
al = noPrime;

statement, inside the solution method, has no affect on the calling code.

One possible fix is

Java
  // ... (as before)
  public static ArrayList<Integer>  removePrimes(ArrayList<Integer> al)
  {
      ArrayList<Integer> noPrime = new ArrayList<>();
      int size = al.size();

        for(int i = 0; i < size ; i++)
        {
            int num = al.get(i);
            if(!isPrime(num))
                noPrime.add(num);
        }

      System.out.println(noPrime);
      return noPrime;

  }
  public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    ArrayList<Integer> al = new ArrayList<>();
    for(int i = 0 ; i < n; i++){
      al.add(scn.nextInt());
    }
    System.out.println("Original = " + al);
    al = removePrimes(al);
    System.out.println(al);
  }

}
   
Comments
Coder Commando 1-Apr-21 7:31am
   
Thank you for the clarification. I understand now.
CPallini 1-Apr-21 7:34am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900