Reputation: 2094
I am getting this error when running this code
private static List<Integer> array(int num) {
List<Integer> value = new ArrayList<>();
while (num != 1) {
if(num % 2 == 0) {
value.add(num + 2);
}else {
value.add(num * 3 + 1);
}
}
System.out.println(value);
return value;
}
Can i get an explanation of whats wrong ?
Upvotes: 1
Views: 192
Reputation: 65
Your while condition never equates to false, so the loop runs endlessly. As shmosie said, you never modify num
, so maybe look into modding num, or performing some operation on it so that it actually equals 1 in order for your loop to stop
Upvotes: 2
Reputation: 111
You have an infinite loop. Each iteration will add to the List until you run OutOfMemory. You need to change num during the loop in such a way that it becomes 1 at some point so the loop ends.
Upvotes: 5