java - 使用 asList() 方法时出现编译器错误
问题描述
我正在尝试使用 Arrays.asList 方法来存储我从 txt 文件中读取的句子。但是,我收到编译错误,告诉我不兼容的类型以及如何没有实例。我假设我不小心遗漏了一些东西,或者我的一条线断了,所以任何帮助都将不胜感激。
public static void main(String[] args) throws IOException
{
Scanner kb = new Scanner(System.in);
// Prompt user to enter file for reading
System.out.print("Please enter the name of a file: ");
String fileName = kb.nextLine();
// Stores user's input file to open
File file = new File(fileName);
// Reads user's file
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext())
{
String str = inputFile.nextLine();
ArrayList<String> list = Arrays.asList(str.split("\\s*,\\s*"));
这是我得到的错误
错误:不兼容的类型:不存在类型变量 T 的实例,因此 List 符合 ArrayList
解决方案
您正在寻找List<String> list = Arrays.asList(str.split("\\s*,\\s*"));
您想获取列表而不是 ArrayList
编辑:抢不回来
推荐阅读
- r - 按分组变量衡量同比滞后
- unit-testing - Alfresco 中基于 Java 控制器的 Web 脚本的单元测试用例
- dexie - dexie js bulkDelete - 参数键 - 数组或对象
- php - 从两个数组创建多维数组
- python - 使用 speech_recognition Python 库进行语音到文本转换会出错
- python - 无法从 Windows 上的 git bash 运行烧瓶应用程序
- javascript - 模板文字写出 HTML 而不是 div 被呈现为 HTML
- elasticsearch - 仅在特定时间运行 ElastAlert
- java - 使用 appium 为 FrameMaker 创建自动化 Windows 测试 - 检查问题
- php - 如何将 /wp-admin 页面重定向到 wordpress 中的另一个页面(不是 wp-login.php)?