0

The program below aims to return all the nonzero values in the array A using the array B.

I am new to Java, but experienced with C++.

Would gladly appreciate any feedback !

public class first 
{
    static int [] A= { 5, 43, 0, 127, 1, 0 ,0 ,3};

    public static int[] NonZeros( int [] A )
    {

        int [] B= new int [10];
        int k=0;
        for(int i=0;i<=7;i++)
            if(A[i]!=0)
            { 
                B[k]=A[i];
                k++;
            }


        return B;
    }
                        public static void main(String[] args)
                        { 

                            System.out.println(NonZeros(A));
                        }
}

Output:

 [I@659e0bfd

1 Answers1

1

Use:

System.out.println(Arrays.toString(NonZeros(A)));

in your main method.

Tom
  • 16,842
  • 17
  • 45
  • 54
Ankush soni
  • 1,439
  • 1
  • 15
  • 30