I'm working on a question where you are supposed return n rows of pascals triangle given and int n and return it as a List of Lists. However I'm getting an index out of bounds exception when I try to call on a previous row and I'm not exactly sure why.
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
List<Integer> row = new ArrayList<>();
for(int i = 0; i < numRows; i++){
for(int j = 0; j <= i; j++){
if(j == 0 || j == i){
row.add(1);
}
else{
if(i != 0 && j != 0){
int num = triangle.get(i-1).get(j-1) + triangle.get(i-1).get(j);
row.add(num);
}
}
}
triangle.add(row);
row.clear();
}
return triangle;
}
I added the if(i != 0 && j != 0) line in hopes of resolving it but the error persists.
Count the amount of
newstatements being executed.I count only 2 new statements, both before your loops even begin.
That means there are 2 array lists. Total.
Given that your code is supposed to return a lot more lists than that, how does this work? Well, java is based on references. All non-primitives values are, in fact, references. It's like a page in an address book, not a house.
List<Integer> row = new ArrayList<>();does 2 things:new ArrayList<>();part)triangle.add(row)will simply create a copy of the address book page, not of the house. You simply write down the address, then go over to the house, smash everything inside (row.clear()), and start over again. You keep refilling the same house, smashing everything in it.triangleis an address book containing many pages, each page having the same address, all leading to a house where someone has repeatedly filled it up, smashed it all to smithereens.This gets us back to: Count the
new.Here you epxect a 5-row sierpinski to have a grand total of 6 lists made: 1 list for each row, and then one more as a list of lists. That means I need to 6 'new' statements being executed.