英文:
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<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.
答案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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论