Kemal P.
Kemal P.

Reputation: 143

Type safety: Unchecked cast from Integer to T

How can fix unchecked cast from Integer to T? Im getting this warning in this line:

undoStackIndexes.push((T)index);

The code:

SimpleStack<T> undoStackIndexes = new SimpleStack<T>();
SimpleStack<T> undoStackDatas = new SimpleStack<T>();
public void add( int idx, T x ){
        Node<T> p = getNode( idx, 0, size( ) );
        Node<T> newNode = new Node<T>( x, p.prev, p );
        newNode.prev.next = newNode;
        p.prev = newNode;         
        theSize++;
        modCount++;

        undoStackDatas.push(newNode.data);
        Integer index = new Integer(idx);
        undoStackIndexes.push((T)index);
    }

Upvotes: 0

Views: 485

Answers (1)

Paweł Chorążyk
Paweł Chorążyk

Reputation: 3643

If you know you're going to store Integer indexes in your undoStackIndexes stack you shouldn't make it generic with T type:

SimpleStack<Integer> undoStackIndexes = new SimpleStack<Integer>();

Upvotes: 2

Related Questions