user1658435
user1658435

Reputation: 574

Finding the last zero's index in an array

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.

Upvotes: 1

Views: 784

Answers (6)

Andrew Templeton
Andrew Templeton

Reputation: 1696

Start from the END of the array and decrement through. Should we never find a zero, this returns -1, which is always outside of the index. This is interpreted by you

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=x.length-1;i>=0;i--){
     if(x[i]==0){
         return i;             
     }
    }
    return -1;
}

}

Upvotes: 0

user1658435
user1658435

Reputation: 574

public static int lastZero(int[] x){
        int temp=0;
        for(int i=0;i<x.length;i++){
         if(x[i]==0){
             temp=i;
         }
        }
        System.out.println(temp);
        return 0;
    }

This would give the desired output.

Upvotes: 0

nbbk
nbbk

Reputation: 1102

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 void findLast(int[] x){
        for(int i=x.length()-1;i>0;i++){
        if(x[i]==0){
            System.out.println(i);
            break;
        }
    }
}

}

Upvotes: 2

sr01853
sr01853

Reputation: 6121

If your constraint is to traverse the array from the start, Store the index as and when you see 0 into a variable and as you complete the loop just print the variable

int lastindex;
for(int i=0;i<x.length;i++){
         if(x[i]==0){
             lastindex=i;
}
System.out.println(lastindex); 

Upvotes: 0

bikeshedder
bikeshedder

Reputation: 7487

Just reverse the loop going from x.length-1 to 0 and return the index on first match:

public static int findLast(int[] x) {
    for (int i=x.length-1; i>=0; i--) {
        if (x[i] == 0) {
            // match found, return index
            return i;
        }
    }
    // no match found, return invalid index
    return -1;
}

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

  • Start at the end of the array (i.e. i=x.length-1)
  • Decrement i rather than incrementing (i.e. use i--)
  • Stop as soon as you reach zero (i.e. add break after println).
  • Make your stopping condition such that the loop processes element at index zero.

Upvotes: 6

Related Questions