简体   繁体   English

LinkedList返回方法

[英]LinkedList returning method

Is it possible to create a method returning a LinkedList and what would be the correct syntax ? 是否可以创建一个返回LinkedList的方法,正确的语法是什么?

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

swap the static and the return type and remove the void : 交换staticreturn type并删除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: 编辑:正如Tunaki提到的,​​最好使用泛型并声明列表中的数据类型:

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

otherwise you get a warning from your IDE 否则,您会从IDE中收到警告

"LinkedList is a raw type. References to generic type LinkedList should be parameterized" “ LinkedList是原始类型。对泛型类型LinkedList的引用应参数化”

the great benefit of that is: 这样做的最大好处是:

  • Stronger type check during compile time 编译期间进行更严格的类型检查
  • you dont need casting. 你不需要铸造。

omit the "void", a void method return nothing ... 省略“ void”,void方法不返回任何内容...

public static LinkedList NewtonRaphson1() {
     return linkedlist;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM