-2

I am having trouble with unchecked casting. I know this happens because T is an Object but not every Object is a T. My question is how would I fix this to implement T in ArrayList<T> without suppressing warnings?

public class AdjMatrix <T extends Object> implements Blah<T>
{
    public T[] vertex;

    public AdjMatrix() 
    {
        this.vertex = (T[])(new Object[RANDOM_NUMBER]);             
    }

    public ArrayList<T> neighbours(T vertLabel) 
    {
        ArrayList<T> neighbours = new ArrayList<T>();

        neighbours.add(vertex[SOME_NUMBER]);

        return neighbours;
    }
}

Thanks for you help!

Tim Davis
  • 1
  • 1

1 Answers1

0

You can type cast to an instance of any class provided you know the type of the class. You can then go ahead and create an instance of the class as: clazz.cast(object)

Prashant
  • 3,210
  • 1
  • 22
  • 35