1

I have successfully implemented Radix sort but I have the following code that I would like to convert to be created by a loop.

private static LinkedList[] bucket = { 
        new LinkedList(), // -9
        new LinkedList(), // -8
        new LinkedList(), // -7
        new LinkedList(), // -6
        new LinkedList(), // -5
        new LinkedList(), // -4
        new LinkedList(), // -3
        new LinkedList(), // -2
        new LinkedList(), // -1
        new LinkedList(), // 0
        new LinkedList(), // 1
        new LinkedList(), // 2
        new LinkedList(), // 3
        new LinkedList(), // 4
        new LinkedList(), // 5
        new LinkedList(), // 6
        new LinkedList(), // 7
        new LinkedList(), // 8
        new LinkedList() // 9
};

However I cannot figure out how I might go about doing this. I tried this but I get compiler errors.

private static LinkedList[] bucket;
int thing = 19;
while(thing != 0){
    bucket = new LinkedList();
    thing--;
}

This is not critical to the functionality of my radix sort as it works flawlessly, I just think it would be cleaner to initialize my buckets with a loop. That said if someone could enlighten me on how to do this I would be very grateful.

1
  • 3
    bucket = new LinkedList(); bucket is an array not a LinkedList. Commented Sep 6, 2014 at 15:11

3 Answers 3

1

Here you go:

private static LinkedList[] bucket = new LinkedList[19];

static {
    for (int i = 0; i < bucket.length; ++i) {
        bucket[i] = new LinkedList();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks that's exactly what I needed. Might I ask though why the ++i instead of i++?
@Azethoth In this scenario, it makes no difference and is simply a matter of style.
In this case it makes no difference. In some cases ++i is more efficient than i++, so I use that as a habit.
Thanks both of you. I did not think it mattered, but on the chance it did I wanted to know why since there is a difference between pre and post fix operations.
The pre-fix operator can update the counter and return its value. The post-fix operator must cache the original value of the counter, increment the counter and return the original value. This caching can be inefficient, depending on the implementation. So if I don't specifically need the original value, I use the pre-fix version.
0

Your LinkedList[] bucket is not initlaized,so you will get NullPointerException at runtime.Also,the syntax of array element assignment is wrong

private static LinkedList[] bucket = new LinkedList[20];
int thing = 19;
while(thing != 0){
    bucket[thing] = new LinkedList();
    thing--;
}

Comments

0

First set up how big the array is going to be (how many LinkedLists are going to be in it), then iterate from zero to that size, and set indices of the array to be new LinkedLists.

For example, if i is your iterator variable, you would say:

bucket[i] = new LinkedList();

Remember what types you're dealing with: a single LinkedList, and an array of them. You can't just assign the entire array to one LinkedList. You have to assign the elements to LinkedLists, to match types.

1 Comment

Thanks for the explanation. I feel silly for forgetting array basics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.