Print an array elements
Posted
by 1ace1
on Stack Overflow
See other posts from Stack Overflow
or by 1ace1
Published on 2010-04-18T07:36:34Z
Indexed on
2010/04/18
7:43 UTC
Read the original article
Hit count: 277
java
Hello guys, this is my first post and im still a beginner in the programming world so bare with me. I just created an array with 100 initialized values and i want to print out 10 elements on each line so it would be somthing like this
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
...26
this is the code i used and i managed to do it for the first 10 elements but i couldnt figure out how to do it for the rest
public static void main(String[] args) {
int[] numbers = { 0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
int i, count = 0;
for (i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
count++;
if (count == 9)
for (i = 9; i < numbers.length; i++)
System.out.println(numbers[i] + " ");
}
}
thanks!
© Stack Overflow or respective owner