文件写入不兼容的类型

时间:2018-05-05 21:54:11

标签: java path incompatibletypeerror

List<Data> arrayListToFile = new ArrayList<>();
arrayListToFile.add(new Data("E30", "1985", ratingE30));
arrayListToFile.add(new Data("E36", "1992", ratingE36));
arrayListToFile.add(new Data("E46", "2000", ratingE46));
arrayListToFile.add(new Data("E92", "2007", ratingE92));
arrayListToFile.add(new Data("F80", "2014", ratingF80));

Path location = Paths.get("src/data.txt");
Charset utf8 = Charset.forName("UTF-8");
try {
    write(location, arrayListToFile, utf8);
} catch (Exception e) {
    System.out.println("The file could not be saved");
}

public static class Data {
    private String name;
    private String year;
    private String rating;

    public Data(String name, String year, String rating) {
        this.name = name;
        this.year = year;
        this.rating = rating;
    }
}

我一直收到错误不兼容的类型:

java.util.List<com.company.Main.Data> cannot be converted to java.lang.Iterable<? extends java.lang.CharSequence>

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:-1)

ArrayList类是List类的扩展,因此如果要使用ArrayList,则在初始化时完全使用它。可能不兼容您可以在构造函数new ArrayList<Data>();

中指定子类的类型
List<Data> arrayListToFile = new ArrayList<Data>();

或者只是将对象初始化为ArrayList

ArrayList<Data> arrayListToFile = new ArrayList<>();