Search code examples
javafor-loopforeachscope

How to avoid declaring variable outside of enhanced for loop


I want to avoid using variable index outside of enhanced for loop as it is useless after the loop and pollutes the namespace but cannot find my way around it.

Problem

int index = 0;        // I want to avoid using this variable outside the loop
for (List<Integer> bucket : buckets) {
    for (int el : bucket) {
        a[index++] = el;
    }
}

Whole Code

package sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BucketSort {
    private BucketSort() {
    }

    private static int getMax(int[] a) {
        int max = Integer.MIN_VALUE;

        for (int el : a) {
            max = Math.max(max, el);
        }

        return max;
    }

    public static void sort(int[] a, int k) {
        int M = getMax(a) + 1;

        List<List<Integer>> buckets = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            buckets.add(new ArrayList<>());
        }

        for (int el : a) {
            int bucketIndex = (int) Math.floor(k * (double) el / M);
            buckets.get(bucketIndex).add(el);
        }

        for (List<Integer> bucket : buckets) {
            Collections.sort(bucket);
        }

        int index = 0;
        for (List<Integer> bucket : buckets) {
            for (int el : bucket) {
                a[index++] = el;
            }
        }
    }
}

One user on a similar question, suggested that a way to avoid the "useless variable" would be to wrap another block around the whole lot for local scoping

{
    int index = 0;        
    for (List<Integer> bucket : buckets) {
        for (int el : bucket) {
            a[index++] = el;
        }
    }
}

but I am not so sure about it.


Solution

  • Just stick it in a method to hide any local variables.

    private void copy(int[] a, List<List<Integer>> buckets) {
        int index = 0;
        for (List<Integer> bucket : buckets) {
            for (int el : bucket) {
                a[index++] = el;
            }
        }
    }
    

    But you are trying to avoid a very common situation that even the best of coders have to accept. I would just leave it as it is or throw the {} around it as previously mentioned.