开发者

Return an int[] after storing in a LinkedList

开发者 https://www.devze.com 2023-02-03 12:17 出处:网络
When I try and return my int[] it says it\'s an object and I cannot return it to int[]. public class CostMatrix {

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[] return开发者_Go百科Cost(int pos){

    return matrix.get(pos)
}

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消