-2

I have written a program that will load an array of 50 random integers ranging between 0 and 128. I am trying to print the array, with only 5 integers printed on each line. But, I can't seem to do it.

public static void printList(int len, int[] list){
        for (int i = 0; i < len; i++) {
            System.out.println(list[i]);
        } // end for 
} // end printList

I want the output to be only 5 integers to a line.

Kevin
  • 16,549
  • 8
  • 60
  • 74
Amy M
  • 1
  • 1
  • What do you mean by 'with only 5 integers printed on each line. '. Does it mean that it you have 50 integers then you will have 10 lines and in each line there will be 5 numbers ? – Deep Oct 23 '19 at 11:04

5 Answers5

3

This may work:

public static void printList(int len, int[] list){
        for (int i = 0; i < len; i++) {
            if (i % 5 ==0) {
                  System.out.println();
            }
            System.out.print(list[i] + "\t");
        } // end for 
} // end printList
Amal K
  • 4,359
  • 2
  • 22
  • 44
2

Reworking @AmalK's code:

public static void printList(int len, int[] list) {
    for (int i = 0; i < len; i++) {
        String sep = "\t"; // usually have a tab after each integer
        if ((i+1) % 5 == 0 || i+1 == len)
            sep = "\n"; // but periodically use a newline instead     
        System.out.print(list[i] + sep);
    } 
}

My changes are: 5 numbers per line; no initial newline; newline after last number even if len is not a multiple of 5; no trailing tab on line.

0

This should be working fine:

public static void printList(int len, int[] list) {
    for (int i = 0; i < list.length; i++) {
        System.out.print(list[i] + " , ");
        if (i % 5 == 0) {
            System.out.println();
        }
    }
}
  • The first line will have only one number on it. The last line may not end in a newline. –  Oct 23 '19 at 12:42
0

if you are certain that the array will always be having number of elements as a multiple of 5, then this can be used.

Its saving you many loop iteration.

public static void printList(int len, int[] list){
        for (int i = 0; i < len; i+=5) {

            System.out.println(list[i] + "\t"+ list[i+1] + "\t"+ list[i+1] +
 "\t"+ list[i+1] + "\t"+ list[i+1] + "\t");
        } // end for 
} // end printList
javaGroup456
  • 313
  • 1
  • 6
  • 21
0

If you're familiar with Guava, you can try this demo. Guava use substring of JDK to do partition, so it won't add too many memory cost.

import com.google.common.collect.Lists;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TestDemo {
    public static void main(String[] args) {
        List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
        List<List<Integer>> partition = Lists.partition(list, 5);
        partition.forEach(x -> System.out.println(x.stream().map(y -> Integer.toString(y)).collect(Collectors.joining(","))));
    }

}
caisil
  • 363
  • 2
  • 14