【问题标题】:create a Generic List创建一个通用列表
【发布时间】:2021-01-16 22:56:07
【问题描述】:
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);
    }
 }

为什么第一个有效而第二个无效?

type 是类UserInput 中的一个方法。在那里我将填充数组。

【问题讨论】:

    标签: java list generics arraylist


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-25
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2012-03-21
      相关资源
      最近更新 更多