Hi Forums! Im in need of some help with two problems im trying to attempt.
So the first problem im doing is the pascal's triangle using arraylist<integer> but when it prints out, it gives me the wrong output.
public ArrayList<Integer> getPascal (ArrayList<Integer> rows) { if (rows.size() < 0) return rows; ArrayList<Integer> lastRow = new ArrayList<Integer>(); lastRow.add(1); System.out.println(lastRow); for (int i = 1; i <= rows.size(); i++) { ArrayList<Integer> currentRow = new ArrayList<Integer>(); currentRow.add(lastRow.get(0)); for (int j = 1; j < i; j++) { currentRow.add(lastRow.get(j - 1) + lastRow.get(j)); } currentRow.add(lastRow.get(i)); lastRow = currentRow; System.out.println(currentRow); } return lastRow; }
From the error compiler there's something wrong with " if (rows.size() < 0) { return; }. Now i know thats obviously wrong but how could i resolve it into ArrayList<Integer> terms? Ive tried a couple methods but they didnt work as well. For my driver, i just declared a new variable to print the result and it gave me [1] [1] going down a column. If anyone does have time on their hands, how would using one object of arraylist look like?
Thank you very much!