0
我正尝试在java中创建一个简单的帮助器类,以便从用户处获取数字列表,并将该List转换为数据操作的基本数组。我坚持如何根据List对象的类类型更改扫描器输入方法的第一步。什么是最好的方法来做到这一点?基于通用类的更改方法
public static <N> List<N> promptList(String promptString, Class<N> resultType){
prompt(promptString);
List<N> inputList = new ArrayList<>();
try (Scanner scanner = new Scanner(inStream)){
do{
inputList.add(scanner.nextDouble()); //switch scanner method based on type of N
} while (true);
} catch (Exception e){
System.out.println("Input complete");
}
return inputList;
}
感谢您的帮助。
你需要检查列表中的Type,然后看看它的'instanceof'是你想要的。 –
对不起,你的问题对我没有意义,因为'N'可以是不同的数字类型。例如:'列表'。实际上,根据您的声明,它甚至可以是'List '...... –
m0skit0