LinkedList返回方法(LinkedList returning method)

编程入门 行业动态 更新时间:2024-10-14 14:16:43
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.

更多推荐

本文发布于:2023-08-07 16:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   LinkedList   method   returning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!