I am writing a radix sort algorithm (that ONLY sorts integer) and have run into a problem, which could possibly be an easy fix, I just can't seem to find it.
I create and array of linked lists here to hold my integers:
LinkedList[] buckets = {new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>(), new LinkedList<Integer>()};
That is the generic linkedlist from the util and I only need it to hold integers. My problem is when I try to run this code
for (int j = 0; j < buckets.length; j++) {
while (!buckets[j].isEmpty()) {
a[pos] = buckets[j].removeFirst();
开发者_Go百科 pos++;
}
On the line where I remove from my "queue" I get the required int found object error. My linkedlists are hold Integers anyway so why does it say that it is an object? Do I have to downcast somewhere or something along those lines?
Thanks.
Look at your declaration:
LinkedList[] buckets
You've declared an array of raw LinkedList
references. You would need to tell the compiler that they'll be LinkedList<Integer>
values:
LinkedList<Integer> buckets = ...;
Unfortunately, arrays and generics don't play terribly nicely together. I suggest you use a List<LinkedList<Integer>>
, like this:
List<LinkedList<Integer>> buckets = new ArrayList<LinkedList<Integer>>();
for (int i = 0; i < 10; i++)
{
buckets.add(new LinkedList<Integer>());
}
Then:
for (LinkedList<Integer> bucket : buckets)
{
while (!bucket.isEmpty())
{
a[pos] = bucket.removeFirst();
pos++;
}
}
精彩评论