on-internet
← Community

Why does my Java array print extra zeros in Eclipse?

Stack Overflow ·

I'm learning Java in Eclipse and practicing arrays. When I run this code, the output is: 10 20 30 0 0 I only added 3 numbers to the array, so I expected it to print only those values. I already checked the loop and the array size, but I still don't understand why the extra zeros appear. Why does Java print those zeros and how can I avoid them? public class Main { public static void main(String[] args) { int[] numbers = new int[5]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; for(int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } } }

Comments

Sign in or sign up to comment and upvote.

No comments yet.