0

I am encountering warnings of unchecked cast. I'm not sure how to resolve the warnings.

The compiler error seems to pick on list = (E[]) new IntKeyed [size];
Which is the code we are suppose to used letter for letter.
We are attempting to use Generics hence the "E".

So I figure I must have goofed somewhere in this code. When I looked up the error, it seemed to deal with ArrayLists, but I'm not using an ArrayList to my knowledge.

Maybe I should be ArrayLists? Would changing that fix the warnings? I also have an issue with incomplete exception handling in my code. But I think that is isolated from the Array issue.

Any errors you can find will help point me in the right direction. Thanks.

public abstract class MyArrayList<E> implements IntKeyed
{

private final int DEFCAP = 50;
private int origCap;
private int numElements;
private E[] list;

public MyArrayList()
{
    origCap = DEFCAP;
    list = (E[]) new IntKeyed [origCap];    *******Error Here
}

public MyArrayList( int size ) throws Exception
{
    size = DEFCAP;
    if( size > 0 )
    {
        list = (E[]) new IntKeyed [size];    *******Error Here
        origCap = size;
    }
    else
    {
        throw new ArrayStoreException();
        System.out.println("List size is invalid");
    }
    list = (E[]) new IntKeyed [size];         *******Error Here
}

private void enlarge ()
{
    E[] larger = (E[]) new IntKeyed [list.length + origCap]; *******Error Here
    for( int i = 0; i < list.length; i++)
    {
        larger[i] = list[i];
    }
    list = larger;
}

When compiling with javac -Xlint:unchecked MyArrayList.java I get:

MyArrayList.java:29: warning: [unchecked] unchecked cast
    list = (E[]) new IntKeyed [origCap];
                 ^
required: E[]
found:    IntKeyed[]
where E is a type-variable:
E extends Object declared in class MyArrayList
MyArrayList.java:37: warning: [unchecked] unchecked cast
        list = (E[]) new IntKeyed [size];
                     ^
required: E[]
found:    IntKeyed[]
where E is a type-variable:
E extends Object declared in class MyArrayList
MyArrayList.java:45: warning: [unchecked] unchecked cast
    list = (E[]) new IntKeyed [size];
                 ^
 required: E[]
found:    IntKeyed[]
where E is a type-variable:
E extends Object declared in class MyArrayList
MyArrayList.java:117: warning: [unchecked] unchecked cast
    E[] larger = (E[]) new IntKeyed [list.length + origCap];
                       ^
required: E[]
found:    IntKeyed[]
where E is a type-variable:
E extends Object declared in class MyArrayList
MyArrayList.java:43: error: unreachable statement
        System.out.println("List size is invalid");
        ^
1 error
4 warnings
user987339
  • 10,519
  • 8
  • 40
  • 45
iYeti
  • 1
  • 1
  • 3

2 Answers2

1

warnings is not a problem, problem is unreachable statement after throwing exception.

it should be this way:

System.out.println("List size is invalid");
throw new ArrayStoreException();

If your believe that your casts should work - add @SuppressWarnings("unchecked") on method or statement level(smaller scope is better). Compiler will not emit any messages, but you will still get exception in runtime if you casts are wrong.

Admit
  • 4,897
  • 4
  • 18
  • 26
1

Warning at list = (E[]) new IntKeyed [origCap]; practically means that generic E is being hardcoded as IntKeyed so whatever you pass for E, your list will always remain of type IntKeyed.

In your case, generic use doesn't seem to be suited as you are already implementing specific interface for this purpose.

harsh
  • 7,502
  • 3
  • 31
  • 32