0

I have a program for a binary search in Java. The 'for-each' loop doesn't seem to increment the counter variable after taking input for the array. However, it does work with a regular 'for' loop. Why can't the 'for-each' loop increment the counter in this situation?

import java.util.Scanner;

public class binarySearch {
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;

        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (key > a[mid])
                lo = mid + 1;
            else if (key < a[mid])
                hi = mid - 1;
            else
                return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the key to be searched");
        int key = in.nextInt();
        System.out.println("\nEnter the number of elements in the array");
        int num = in.nextInt();
        int[] array = new int[num];

        for (int counter : array) {
            System.out.println("Enter the element of the array!");
            array[counter] = in.nextInt();
        }
        int result = rank(key, array);
        if (result == -1) {
            System.out.println("\n The given key is not found!\n");
        } else {
            System.out.println("\n The given key is found at position : " + (result + 1));
        }
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Karthik Bhat
  • 361
  • 1
  • 4
  • 11
  • 1
    As an aside to help with future questions: the binary search part isn't actually relevant here. Just printing out the array after populating it would show the problem. It's best to keep questions to a minimal example that demonstrates the problem. Not a big issue in this question, but worth knowing for future ones. – Jon Skeet Jan 23 '19 at 09:43
  • Yeah, sure thanks! – Karthik Bhat Jan 23 '19 at 09:45
  • 1
    Have a look at this https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work Check out the links mentioned in the answer as well as the comments to get a better idea on how for-each works. Hope this helps. – Ben Jan 23 '19 at 09:36

3 Answers3

5

You've just created the array without populating it, so it will be full of default values. You're then iterating over the values of the elements of the array, which means that the value of counter is going to be 0 every time. This loop:

for(int counter : array )
{
    System.out.println("Enter the element of the array!");
    array[counter] = in.nextInt();
}

... is broadly equivalent to this:

for (int i = 0; i < array.length; i++) {
    // Note: this will always be zero because the array elements are all zero to start with
    int counter = array[i]; 
    System.out.println("Enter the element of the array!");
    array[counter] = in.nextInt();
}

You don't actually want to iterate over the original values in the array at all - you just want to iterate from 0 to the length of the array (exclusive) which is easily done with the for loop:

for (int i = 0; i < array.length; i++) {
    System.out.println("Enter the element of the array!");
    array[i] = in.nextInt();
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The foreach loop does not iterate over the array indexes but over the array elements.

Donat
  • 4,157
  • 3
  • 11
  • 26
0

Because in

    for(int counter : array )
    {
        System.out.println("Enter the element of the array!");
        array[counter] = in.nextInt();
    }

counter is not a counter. It is value from array.

talex
  • 17,973
  • 3
  • 29
  • 66