简体   繁体   中英

LinkedList returning method

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

public LinkedList  static void NewtonRaphson1() {
         return 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.

omit the "void", a void method return nothing ...

public static LinkedList NewtonRaphson1() {
     return linkedlist;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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