【发布时间】:2022-01-04 16:06:56
【问题描述】:
我正在尝试将现有 csv 文件中的数据添加到 java 数组,但我似乎没有找到解决方法。
这是我多次尝试后遇到的错误。 错误:无法为最终变量 addPlatform 赋值 addPlatform = new ArrayList();
这是我的 main.java
public class Game {
public static void main(String[] args) {
Game game = new Game("platforms.csv");
game.run();
}
@Getter(lazy = true)
private final List<Platform> addPlatform = new ArrayList<Platform>();
/**
* Reads platforms from csv file and returns the as list.
* @return platforms - Platforms as list
*/
private List<Platform> readPlatforms() {
if (addPlatform == null) {
addPlatform = new ArrayList<Platform>();
}
return addPlatform;
}
平台.java
package com.nortal.platformer;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Platform {
private Integer index;
private Integer cost;
}
平台.csv
index, cost
0, 100
1, 200
2, 400
【问题讨论】:
-
我有一个 Game.java 类和一个文件platforms.csv。我正在尝试将 csv 文件中的所有行添加到列表中(在 Game.java 中)``` /** * 从 csv 文件中读取平台并将它们作为列表返回。 * @return 平台 - 平台列表 */ private List
readPlatforms() { return addPlatform; } ```
标签: java arrays collections lombok