Jon
Jon

Reputation: 739

Return an int[] after storing in a LinkedList

When I try and return my int[] it says it's an object and I cannot return it to int[].

public class CostMatrix {

LinkedList matrix = new LinkedList<int[]>();

CostMatrix() {
}

void addToMatrix(int[] toAdd) {
    matrix.add(toAdd);
}

int[] returnCost(int pos){

    return matrix.get(pos)
}

}

Upvotes: 0

Views: 111

Answers (1)

Mike Samuel
Mike Samuel

Reputation: 120546

You're missing a type parameter on the declaration.

LinkedList matrix -> LinkedList<int[]> matrix

To understand why you get this error, search for "raw types" in http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html

Basically, your code creates a correctly parameterized list but assigns it to a property with a raw type.

Then when you get from it, the compiler only knows that the property is a LinkedList and assumes that its content can be any type of Object.

Upvotes: 3

Related Questions