Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 idiom. I would just leave it as it is or throw the {} around it as previously mentioned.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading