I had to return ArrayList
of ArrayList<Integer>
as it was predefined.
I was trying this code( for pascal triangle of n lines):
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> b = new ArrayList<Integer>();
for(int i=1;i<=n;i++){
int c=1;
//ArrayList<Integer> b = new ArrayList<Integer>(); I declared
//the arraylist here to solve the problem
// Place 1: b.clear()
for(int j=1;j<=i;j++){
b.add(c);
c=(c*(i-j))/j;
}
a.add(b);
// Place 2: b.clear();
}
return a;
Expected output:
n=4
: [1 ] [1 1 ] [1 2 1 ] [1 3 3 1 ]
I wanted to know why i was not getting the desired output when i placed clear functions at:
Place 1 output: [1 3 3 1 ] [1 3 3 1 ] [1 3 3 1 ] [1 3 3 1 ]
(Only the last row)
Place 2: [ ] [ ] [ ] [ ]
(no output)
The same was the case with removeAll()
.