创建一个通用列表

huangapple go评论70阅读模式
英文:

create a Generic List

问题

public class Sortieralgorithmen<T extends Comparable<T>>
{
    public static void main (String[] args) throws IOException
    {
         UserInput read = new UserInput();
         ArrayList<Integer> list = read.type(muster, type, length, rounds);
         ArrayList<T> list = read.type(muster, type, length, rounds);
    }
 }
 
Why is the first working and the second is not?

`type` is a method in the class `UserInput`. There I will fill the Array.
英文:
public class Sortieralgorithmen&lt;T extends Comparable &lt;T&gt;&gt;
{
    public static void main (String[] args) throws IOException
    {
         UserInput read = new UserInput();
         ArrayList&lt;Integer&gt; list = read.type(muster, type, length, rounds);
         ArrayList&lt;T&gt; list = read.type(muster, type, length, rounds);
    }
 }

Why is the first working and the second is not?

type is a method in the class UserInput. There I will fill the Array.

答案1

得分: 2

你的类有一个类型参数 T,但是你的 main 是一个静态方法。

就像非静态字段一样,类型参数 T 只能在非静态方法中访问(因为在静态方法中没有实例可以从中获取类型 T)。

英文:

Your class has a type parameter T, but your main is a static method.

Just like non-static fields the type parameter T is only accessible in non-static methods (as there is no instance to "take" the type T from in static methods).

huangapple
  • 本文由 发表于 2020年10月1日 16:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定