public class ApiClass{
//The below method prints only the user entered values
public void printArray(int[] a){
for(int element : a){
//ignore because this is the default value when array is created not user entered
if(element != 0)
{
System.out.print(element+"\t");
}
}
}
}
public class Client{
public static void main(String... args){
ApiClass api = new ApiClass();
int[] input = new int[5];
input[0]= 3;
input[2]= 2;
input[3] = 1;
api.printArray(input);
}
}
This is working fine but failing for the 3,2,0 or 0,0,0 or 3,0,1 any input user enters with zero
Satya:
If your problem is simply to distuingish the default 0-values from user-entered 0-values, why dont you initialize the array with a value that you know a user can never enter? (You need to make sure that this is the case).
One way is to iterate the array before and initialize, another one is to use
Arrays.fill()