[英]Passing 2D ArrayLists in Java
Can someone show me how to set up getters and setters for this 2D ArrayList? 有人可以告诉我如何为该2D ArrayList设置吸气剂和吸气剂吗?
public class PureRatings {
private List<List<Integer>> pureRatingsList;
I'm not sure if this part is right... 我不确定这部分是否正确...
public PureRatings() throws IOException {
pureRatingsList = parseRatingsFile();
}
Here is the code for the rest of the 2D ArrayList, I didn't know if I should include it or not... 这是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;
}
} }
You would get an entry like this: 您将获得如下条目:
pureRatingsList.get(line).get(column);
You would set an entry like this: 您将这样设置一个条目:
pureRatings.get(line).set(column, newValue);
public void setPureRatingsList(List<List<Integer>> lst)
{
pureRatingsList = lst;
}
public List<List<Integer>> getPureRatingsList()
{
return Collections.unmodifiableList(pureRatingsList);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.