Can not return an array in java? -
this question has answer here:
- what's simplest way print java array? 24 answers
the program below aims return nonzero values in array using array b.
i new java, experienced c++.
would gladly appreciate feedback !
public class first { static int [] a= { 5, 43, 0, 127, 1, 0 ,0 ,3}; public static int[] nonzeros( int [] ) { 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
use:
system.out.println(arrays.tostring(nonzeros(a)));
in main
method.
Comments
Post a Comment