有人可以告诉我如何为这个 2D ArrayList 设置 getter 和 setter 吗?
public class PureRatings {
private List<List<Integer>> pureRatingsList;
我不确定这部分是否正确......
public PureRatings() throws IOException {
pureRatingsList = parseRatingsFile();
}
这是 2D ArrayList 其余部分的代码,我不知道是否应该包含它......
public static List<List<Integer>> parseRatingsFile() throws IOException {
List<List<Integer>> pureRatings = new ArrayList<List<Integer>>();
BufferedReader in = new BufferedReader(new FileReader("Ratings.txt"));
String ratingsLine = null;
while ((ratingsLine = in.readLine()) != null) {
pureRatings.add(parseRatingsLine(ratingsLine));
}
in.close();
return pureRatings;
}
public static List<Integer> parseRatingsLine(String ratingsLine) throws IOException {
List<Integer> ratings = new ArrayList<Integer>();
if (ratingsLine == null) {
return ratings;
}
String[] ratingsStrArr = ratingsLine.split(" ");
try {
for (final String ratingStr : ratingsStrArr) {
ratings.add(Integer.parseInt(ratingStr));
}
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
return ratings;
}
}
最佳答案
public void setPureRatingsList(List<List<Integer>> lst)
{
pureRatingsList = lst;
}
public List<List<Integer>> getPureRatingsList()
{
return Collections.unmodifiableList(pureRatingsList);
}
关于java - 在 Java 中传递 2D ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9013970/