+ 1
Need a lil help
Guys plis tell me why. In the code when i comment out line 30. I got OutOfMemory error https://code.sololearn.com/cI7bzUFn0sv8/?ref=app
7 Answers
+ 4
when i=1
list.add(s1); cause "pull" to be added to list at end and size increased. size=3
when i=3
list.add(s1); cause "pull" to be added to list at end and size increased. size=4
when i=4
list.add(s1); cause "pull" to be added to list at end and size increased. size=5
This gets repeated.
But if you remove, size won't change because you adding again so size-1+1. When I=3 loop terminated
+ 1
Your loop will run into infinite times as adding to list.add("pull") cause list.size() value also gets increased because if condition is always true. And it repeats every iteration....
if you remove and add, then list size is 3 only..
+ 1
stephen haokip
You are removing then adding same thing so every time size will be same.
If you will not remove then list size will be increase and result would be OutOfMemory
+ 1
If you add list size is also increased!!
0
Jayakrishna How does list.size() increase... i just know that when we remove something in a list the size decrease...and when we add it increase... But increasing infinitely... I dont get it. Plis explain
0
stephen haokip
You are removing and adding same thing why? I didn't get.
0
I know that but when only i add ... I got error