public static List> generate(int numRows) { List> list = new ArrayList<>(); for (int i = 0; i < numRows; i++) { List tmpList = new ArrayList<>(); for (int j = 0; j <= i; j++) { int elem = 1; if (j > 0 && j < i) { List lastRow = list.get(list.size() - 1); elem = lastRow.get(j) + lastRow.get(j - 1); } tmpList.add(elem); } list.add(tmpList); } return list; }