Home > database >  Why I am getting 0 at the first index of my newArray in Java?
Why I am getting 0 at the first index of my newArray in Java?

Time:11-01

I made a program to find the same element in two arrays(only once) and take those element in new Array. It is running fine but don't know why I am getting 0 at the first index of my new arary. suppose if the answer should like [9,8] but it is printing [0,9,8]! Can you correct me where I am going wrong?

newArray size should be 2 but I know it should have started with 0 but I am doing this because if I am starting newArray size with 0 - array out of bound exception will occur.

package Arrays;
import java.util.Arrays;

public class InteractionOfTwoArrays {

    public static void main(String[] args) {
        
        int arr1[]= new int[] {6,9,8,5};
        int arr2[]= new int[] {9,2,4,1,8};
        
        intersections(arr1,arr2);
    }
    public static void intersections(int arr1[], int arr2[])
    {
        int newArraysize=1;
        for(int i=0; i<arr1.length; i  )
        {
            // for getting size of the array
            
            for(int j=0; j<arr2.length; j  )
            {
                if(arr1[i]==arr2[j])
                {
                    System.out.println(arr1[i] " and " arr2[j] " match!");
                    newArraysize  ;
                    System.out.println(newArraysize);
                }
                                    
            }
        }
        
        System.out.println(newArraysize);
        
        int newArray[] = new int[newArraysize];
        
        
        
        for(int i=0; i<arr1.length; i  )
        {       
            
            for(int j=0; j<arr2.length;j  )
            {
                if(arr1[i]==arr2[j])
                {
                    newArray[i] = arr1[i];
                    System.out.println(arr1[i] " moved to "  newArray[i]);
                    break;
                }
                    
            }
        }
        
        System.out.println(Arrays.toString(newArray));      
    }

}

I have tried to print where I am going wrong but failed to identify.

CodePudding user response:

Well, you think java does automatically put values at certain indexes of an array? Or if you want to create an array of size 2, it automatically creates an array of size 3 and put a 0 at first index?

Obviously, no. It is doing exactly what you are instructing it to do. Think about it and debug your code. You already are doing System.out.println(newArraysize); , so did you notice that your new array size is calculated 3, whereas it should be 2 because there are only two matching elements in the above arrays? Have you thought what could be the cause of it?

Secondly, when you are doing newArray[i] = arr1[i]; you can stop-debug at this line and see the value of i. Why is it setting the values at 1 and 2 indexes of newArray, where it shsould set values at 0 and 1 index?

So, as the comments on your question suggest, it is not a generic question, rather you are not doing something correct in your logic. Somebody may paste corrected logic here in answers, but that is not purpose of this site.

CodePudding user response:

The working version of your code is below. I commented out the description in correspondent lines. But as for reference:

  • The newArray which keeps the common elements of other two arrays is empty at the first. int newArraysize=1 was causing the unwanted first 0 element in the final array.

  • you need to keep track of the newArray's index separately and not relying on indexes of other arrays. Because when you're looping through arr1 and find a common element at the index 3 of arr1, you were trying to save that element at the same index (3) of newArray too. While it's possible that there are only 2 common elements and the newArray doesn't have the index 3 at all.

    import java.util.Arrays;

    public class InteractionOfTwoArrays {

     public static void main(String[] args) {
    
         int arr1[]= new int[] {6,9,8,5};
         int arr2[]= new int[] {9,2,4,1,8};
    
         intersections(arr1,arr2);
     }
     public static void intersections(int arr1[], int arr2[])
     {
         int newArraysize=0; // <<< Array is empty at first, so the size should be 0 and then get incremented with each match found
         for(int i=0; i<arr1.length; i  )
         {
             // for getting size of the array
    
             for(int j=0; j<arr2.length; j  )
             {
                 if(arr1[i]==arr2[j])
                 {
                     System.out.println(arr1[i] " and " arr2[j] " match!");
                     newArraysize  ;
                     System.out.println(newArraysize);
                 }
    
             }
         }
    
         System.out.println(newArraysize);
    
         int newArray[] = new int[newArraysize];
    
         int k = 0; // <<< The index counter for the newArray
    
         for(int i=0; i<arr1.length; i  )
         {       
    
             for(int j=0; j<arr2.length;j  )
             {
                 if(arr1[i]==arr2[j])
                 {
                     newArray[k  ] = arr1[i]; // <<< The index of newArray gets incremented each time.
                     break;
                 }
    
             }
         }
    
         System.out.println(Arrays.toString(newArray));      
     }
    

    }

  • Related