+ 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

21st Nov 2021, 4:46 PM
stephen haokip
stephen haokip - avatar
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
21st Nov 2021, 5:47 PM
Jayakrishna 🇮🇳
+ 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..
21st Nov 2021, 4:54 PM
Jayakrishna 🇮🇳
+ 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
21st Nov 2021, 5:09 PM
A͢J
A͢J - avatar
+ 1
If you add list size is also increased!!
21st Nov 2021, 5:39 PM
Jayakrishna 🇮🇳
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
21st Nov 2021, 5:00 PM
stephen haokip
stephen haokip - avatar
0
stephen haokip You are removing and adding same thing why? I didn't get.
21st Nov 2021, 5:07 PM
A͢J
A͢J - avatar
0
I know that but when only i add ... I got error
21st Nov 2021, 5:11 PM
stephen haokip
stephen haokip - avatar