LinkedList返回方法(LinkedList returning method)

是否可以创建一个返回LinkedList的方法,以及正确的语法是什么?

public LinkedList static void NewtonRaphson1() { return linkedlist; }

Is it possible to create a method returning a LinkedList and what would be the correct syntax ?

public LinkedList static void NewtonRaphson1() { return linkedlist; }

最满意答案

交换static和return type并删除void :

public static LinkedList<String> NewtonRaphson1() { return new LinkedList<String>(); }

你定义了错误的语法。

编辑:正如Tunaki所说,最好使用泛型并声明列表中的数据类型:

LinkedList<String> LinkedList<Integer> LinkedList<Float> LinkedList<MyObjectClass>

否则你会从IDE收到警告

“LinkedList是一种原始类型。应该参数化对泛型类型LinkedList的引用”

这样做的好处是:

在编译期间进行更强的类型检查 你不需要铸造。

swap the static and the return type and remove the void:

public static LinkedList<String> NewtonRaphson1() { return new LinkedList<String>(); }

you have defined a wrong syntax.

Edit: As Tunaki mentioned it is better to use generic and declare the type of data that is in your list:

LinkedList<String> LinkedList<Integer> LinkedList<Float> LinkedList<MyObjectClass>

otherwise you get a warning from your IDE

"LinkedList is a raw type. References to generic type LinkedList should be parameterized"

the great benefit of that is:

Stronger type check during compile time you dont need casting.
LinkedList返回方法(LinkedList returning method)

是否可以创建一个返回LinkedList的方法,以及正确的语法是什么?

public LinkedList static void NewtonRaphson1() { return linkedlist; }

Is it possible to create a method returning a LinkedList and what would be the correct syntax ?

public LinkedList static void NewtonRaphson1() { return linkedlist; }

最满意答案

交换static和return type并删除void :

public static LinkedList<String> NewtonRaphson1() { return new LinkedList<String>(); }

你定义了错误的语法。

编辑:正如Tunaki所说,最好使用泛型并声明列表中的数据类型:

LinkedList<String> LinkedList<Integer> LinkedList<Float> LinkedList<MyObjectClass>

否则你会从IDE收到警告

“LinkedList是一种原始类型。应该参数化对泛型类型LinkedList的引用”

这样做的好处是:

在编译期间进行更强的类型检查 你不需要铸造。

swap the static and the return type and remove the void:

public static LinkedList<String> NewtonRaphson1() { return new LinkedList<String>(); }

you have defined a wrong syntax.

Edit: As Tunaki mentioned it is better to use generic and declare the type of data that is in your list:

LinkedList<String> LinkedList<Integer> LinkedList<Float> LinkedList<MyObjectClass>

otherwise you get a warning from your IDE

"LinkedList is a raw type. References to generic type LinkedList should be parameterized"

the great benefit of that is:

Stronger type check during compile time you dont need casting.