1

Hey, I'm trying to implement the ShellSort algorithm and now I got a problem:

warning: [unchecked] unchecked cast
found : java.util.Vector
required: java.util.Vector<java.lang.Object>
Vector<Object> vcur = (Vector<Object>)currentCols[tcur];
Same with vtmp.

I don't know where the problem is. It would be really great if you could help me. :)
This is my code:

public static Vector<Object> shellSort(Vector<Object> ul) {
    int lcount = ul.size();
    int colcount = 4; // 2^x
    Vector[] currentCols = { ul };
    for(; colcount > 0; colcount = (colcount / 2)) {
        Vector[] tmpCols = new Vector[colcount];
        for(int t1 = 0; t1 < colcount; t1++) {
            tmpCols[t1] = new Vector<Object>();
        }
        int tcur = 0;
        int tcurlvl = 0;
        int ttmp = 0;
        for(int t2 = 0; t2 < lcount; t2++) {
            Vector<Object> vcur = (Vector<Object>)currentCols[tcur];
            Vector<Object> vtmp = (Vector<Object>)tmpCols[ttmp];
            vtmp.addElement((Object)vcur.elementAt(tcurlvl));

            // step to next place
            tcur++;
            ttmp++;
            if(tcur == currentCols.length) { tcur = 0; tcurlvl++; }
            if(ttmp == tmpCols.length) { ttmp = 0; }
        }
    }
    return ul;
}
papr
  • 4,717
  • 5
  • 30
  • 38

3 Answers3

1

I believe the problem here is type erasure. At run-time, Vector is simply Vector. For example:

Vector<String> stuff = new Vector<String>()
Vector<Object> objects = (Vector<Object>)stuff

It will compile, but fail at runtime when you try to put an Int into objects.

Collin
  • 1,431
  • 12
  • 19
  • Java doesn't let you cast like that, last i checked -- for exactly the reason you've given. – cHao Nov 06 '10 at 17:30
  • Apparently it's letting him cast like that, but giving him an unchecked cast warning? – Collin Nov 06 '10 at 17:32
  • More likely the warning is about the `Vector[]` declarations in the code. `Vector[]` and `Vector[]` may look alike in bytecode, but they're not exactly the same to the compiler. – cHao Nov 06 '10 at 17:35
0

Why are you declaring currentCols?

Vector[] currentCols = { ul };

This array always contains one element - the ul. Just replace currentCols[..] with ul and the error will also go away.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

This still won't fix the issue (you code doesn't work... never sorts) but it is updated to use List instead of Vector (Vector is obsolete, cannot be deprecated since official APIs depend on it). Unless you need the synchronization built into Vector use List/ArrayList instead.

I also changed the array creation to be closer to generic (cannot truly do generic array creation).

Finally I made the whole method generic so that it can work typesafely with things other than Object (so a List or List, etc...).

It still gives warnings though. Perhaps you should work on getting the code to work properly before you worry about this particular warning (normally I would not suggest that, but with generics that can sometimes be a better way to do it).

public static <T> List<T> shellSort(List<T> ul) 
{
    int lcount = ul.size();
    int colcount = 4; // 2^x

    List<T>[] currentCols = (List<T>[])Array.newInstance(List.class, 1);
    currentCols[0] = ul;

    for(; colcount > 0; colcount = (colcount / 2))
    {
        List<T>[] tmpCols = (List<T>[])Array.newInstance(List.class, colcount);

        for(int t1 = 0; t1 < colcount; t1++)
        {
            tmpCols[t1] = new ArrayList<T>();
        }

        int tcur = 0;
        int tcurlvl = 0;
        int ttmp = 0;

        for(int t2 = 0; t2 < lcount; t2++)
        {
            List<T> vcur = currentCols[tcur];
            List<T> vtmp = tmpCols[ttmp];
            vtmp.add(vcur.get(tcurlvl));

            // step to next place
            tcur++;
            ttmp++;
            if(tcur == currentCols.length) { tcur = 0; tcurlvl++; }
            if(ttmp == tmpCols.length) { ttmp = 0; }
        }
    }

    return ul;
}
TofuBeer
  • 60,850
  • 18
  • 118
  • 163
  • After trying your code and fixing it a bit there it is again: warning: [unchecked] unchecked cast found : java.lang.Object required: java.util.List[] List[] tmpCols = (List[])Array.newInstance(List.class, colcount); – papr Nov 06 '10 at 18:29
  • Yup. There isn't a way to remove the complaint (that I know of) but this code is closer to what you are after (even if you just take the Array.newInstance code). Generally a compiler warning is a bad thing, in this case, since there isn't a way around it, you just have to accept it. – TofuBeer Nov 06 '10 at 19:10
  • Your use of Array.newInstance is incorrect. You are creating an array of 'List's. Even a simple new Object[size]; would have been better – Marco Servetto May 05 '20 at 05:21