user319570
user319570

Reputation: 45

Print an array elements with 10 elements on each line

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 couldn't 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] + " ");
    }
}

Upvotes: 2

Views: 63962

Answers (4)

ilkin
ilkin

Reputation: 21

I know this is an old question, but I just wanted to answer. In java 8 you can do this in one line. Arrays.stream(arr).forEach(s->System.out.print(s%10 > 0 ? s+" ":"\n"));

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383746

    int[] numbers = new int[100];
    for (int i = 0; i < numbers.length; i++) {
        if (i % 10 == 0 && i > 0) {
            System.out.println();
        }
        System.out.print(numbers[i] + " ");
    }

This prints a newline before printing numbers[i] where i % 10 == 0 and i > 0. % is the mod operator; it returns the remainder if i / 10. So i % 10 == 0 when i = 0, 10, 20, ....


As for your original code, you can make it work with a little modification as follows:

int count = 0;
for (int i = 0; i < numbers.length; i++) {
   System.out.print(numbers[i] + " ");
   count++;
   if (count == 10) {
     System.out.println();
     count = 0;
   }
}

Basically, count is how many numbers you've printed in this line. Once it reaches 10, you print the newline, and then reset it back to 0, because you're starting a new line, and for that line, you haven't printed any numbers (yet).


Note that in above two solutions, an extra space is printed at the end of each line. Here's a more flexible implementation that only uses separators (horizontal and vertical) when necessary. It's only slightly more complicated.

static void print(int[] arr, int W, String hSep, String vSep) {
    for (int i = 0; i < arr.length; i++) {
        String sep =
            (i % W != 0) ? hSep :
            (i > 0)      ? vSep :
            "";
        System.out.print(sep + arr[i]);
    }
    System.out.println(vSep);
}

If you call this say, as print(new int[25], 5, ",", ".\n");, then it will print 25 zeroes, 5 on each line. There's a period (.) at the end of each line, and a comma (,) between zeroes on a line.

Upvotes: 5

rsp
rsp

Reputation: 23373

Why do you use 2 nested loops where you only need to remember at which places you need to output a linebreak? Also using the same variable i for both loops will not do what you expect.

How about:

for (i = 0; i < numbers.length; i++) {

    System.out.print(numbers[i] + " ");
    count++;

    if (count == 10)
        System.out.print("\n");
        count = 0;
    }
}

Upvotes: 3

Matthew T. Staebler
Matthew T. Staebler

Reputation: 4996

All you are going to have to do is to print out a newline after every ten numbers.

for (i = 0; i < numbers.length; ++i)
{
    System.out.print(number[i]);
    if (i % 10 == 9)
    {
        System.out.println();
    }
    else
    {
        System.out.print(" ");
    }
}

Upvotes: 1

Related Questions