在Java中传递2D ArrayLists

时间:2012-01-26 04:32:48

标签: java arraylist

有人可以告诉我如何为这个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;
}

}

2 个答案:

答案 0 :(得分:2)

你会得到这样的条目:

pureRatingsList.get(line).get(column);

您可以设置如下条目:

pureRatings.get(line).set(column, newValue);

答案 1 :(得分:2)

public void setPureRatingsList(List<List<Integer>> lst)
{
  pureRatingsList = lst;
}

public List<List<Integer>> getPureRatingsList()
{
  return Collections.unmodifiableList(pureRatingsList);
}