0

All the code is correct aside from the lines where I have the array of numbers and try to print them (lines 5-6). How do I print the array?

public class PassedArray {

public static void passingArray(int[] nums) {

  int arr[] = {6, 3, 8, 89, 34, 89, 132, 76, 34, 89, 11, 9, 19} ; 
         System.out.println(nums);

int[] temp = new int[nums.length]; //create temporary array, temp, set the length of it equal to the original array
for(int i = 0; i < nums.length; i++) {
temp[i] = nums[i]; //set every value at temp equal to every number at i
//return nums;  
     }
int index = 0; //create integer index to keep track of index
for(int i = 0; i < temp.length; i++) {
    if(temp[i] % 2 ==0) { //if number at i divided by 2 yields remainder of 0
        nums[index] = temp[i]; //set number at index equal to temp at i, because temp at i is even
        index++; //Increment of index++ means index will increase by one each loop
                 //This is done because if this is not done, only the first number will be replaced 
    }
}

 for (int i = 0; index < temp.length; index++) {
     if (temp[i] % 2 != 0) { //if number at i divided by 2 does not yield remainder of 0
         nums[index] = temp[i]; //add the temp value to nums at index
         index++; //Increment of index++ means index will increase by one each loop
         
     }
 }
    
}

}

 
hani
  • 49
  • 1
  • 7
coder876
  • 1
  • 2
  • You should write a loop statement to iterate on array element and print them – Seyed Mohammad Amin Atyabi Dec 05 '21 at 05:16
  • 1
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Hussien Mostafa Dec 05 '21 at 05:17
  • I assume you mean where your system.out.println(nums) because I can't see line numbers. But doing it that way will give you the hashcode. Use Arrays.toString(nums) – Femke Dec 05 '21 at 05:26

1 Answers1

1

System.out.println(nums) only gives you the hashcode. Use Arrays.toString(nums)

Femke
  • 85
  • 8