Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change an integer array within a method

I am having an issue with a fill-in-the-code digital textbook problem. All the code is permanent and cannot be changed, so the problem can only be solved by using the area that states //Write code here.

The problem asks to implement the removeOdd method.

import java.util.Arrays;

public class RemoveTester
{
   public static int removeOdd(int[] values, int size) 
   { 
      //Write code here
   }
   public static void main(String[] args)
   {
      int[] a = { 22, 98, 95, 46, 31, 53, 82, 24, 11, 19 };
      int sizeBefore = 8;
      int sizeAfter = removeOdd(a, sizeBefore);
      System.out.print("a: [ ");
      for (int i = 0; i < sizeAfter; i++) 
      {
         System.out.print(a[i] + " ");
      }
      System.out.println("]");
      System.out.println("Expected: [ 22 98 46 82 24 ]");      

      int[] b = { 23, 97, 95, 45, 31, 53, 81, 24, 11, 19 };
      sizeBefore = 7;
      sizeAfter = removeOdd(b, sizeBefore);
      System.out.print("b: [ ");
      for (int i = 0; i < sizeAfter; i++) 
      {
         System.out.print(b[i] + " ");
      }
      System.out.println("]");
      System.out.println("Expected: [ ]");      
   }
}

The way I tried to implement removeOdd is by doing:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int evenCount = 0;
      
      for(int i = 0; i<size; i++){
         if(values[i]%2==0){
            evenCount++;
         }
      }
      int[] newValues = new int[evenCount];
      int newCount =0;
      for(int i = 0; i<evenCount; i++){
         if(values[i]%2==0){
            newValues[newCount] = values[i];
            newCount++;
         }
      }
      values = newValues;
      return evenCount;

When the program is compiled and ran, main prints the beginning of the original a or b arrays instead of only the even elements in a or b. I cannot find a way to alter the original arrays within the method removeOdd into the new arrays with only their even elements. I can’t think of any other way to do this either. Any help would be greatly appreciated!

>Solution :

Because Java is pass by value and not pass by reference, setting the value of the values argument will not change the value of the a variable.

What you have to do is remove all the odd elements from the array and shift the remaining even elements to the left so that the actual resultant form of a looks like this:

{22, 98, 46, 82, 24, 0, 0, 0, 0, 0}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading