Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to implement bucket sort and I want to create an arraylist for each index of an array. I want to do something like this:

int bucket[]=new int[max+1];
for(i=0;i<=max/5;i++)
{
    bucket[i]=new ArrayList<Integer>();
}

But the above code doesn't work and throws an error. It states "generic array creation".I know that generic array types are not allowed in java but I can't understand where generic types comes here I mean something like or .I also want to know is there a way to cast Integer to int while creating the ArrayList I hope doing so will fix this.

share|improve this question
    
You don't need to specify the size of an array list. ArrayList<Integer>[5] just ArrayList<Integer>() –  Jorge Campos 2 days ago
    
@JorgeCampos Yeah I know that but If I use that it says '] expected' –  user1613360 2 days ago
    
Candroid had catch the problem... I've missed the types.. –  Jorge Campos 2 days ago
    
bucket must be of type ArrayList<Integer>[] –  hoosssein 2 days ago
    
@user1613360 i have posted an answer tell me if it is not you are looking for..i will delete it. –  Deepanshu Bedi 2 days ago

1 Answer 1

up vote 1 down vote accepted

Your data types do not match...

You are trying to put an ArrayList<Integer> into a int[]. It cannot hold this type of data!

please reconsider what you want to have:

int[][]
ArrayList<int[]>
ArrayList<ArrayList<Integer>>
ArrayList<Integer>[]  <--- thi is not well-supported due to the way generics work

If you want higher performance, you should have a look at GNU Trove, and maybe use

ArrayList<TIntList>

which should use much less memory than ArrayList<ArrayList<Integer>>.

If your data size is fixed, you probably are looking for

int[][] bucket = new int[max+1][max/5+1];
share|improve this answer
    
I changed it.It doesn't help gives the same error and it's not an answer. –  user1613360 2 days ago
    
See the lower part. Your problem is that int[] bucket cannot hold ArrayList<Integer>, obviously. –  Anony-Mousse 2 days ago
    
Can you please explain why? –  user1613360 2 days ago
2  
Because it can hold int only. Not ArrayLists. It's an array-of-integers, not an array-of-ArrayLists-of-Integers. –  Anony-Mousse 2 days ago
    
I just wonder why you're using ArrayList<ArrayList<Integer>> instead of showing to use List<List<Integer>> list = new ArrayList<List<Integer>>(); –  Zhuinden 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.