I wrote the following code for finding the last zero index in an array:
public class Stack {
public static void main(String[] args){
int[] a=new int[5];
a[0]=1;
a[1]=0;
a[2]=90;
a[3]=0;
a[4]=4;
findLast(a);
}
public static int findLast(int[] x){
for(int i=0;i<x.length;i++){
if(x[i]==0){
System.out.println(i);
}
}
return 0;
}
}
The output is as follows:
1
3
What I really want is the index 3.
i=x.length-1)irather than incrementing (i.e. usei--)breakafterprintln).