简体   繁体   中英

How to break a one-dimensional array in rows?

I want to break a one-dimensional array in rows. Array dimension 50. I need to output the array to the console with 10 elements per line. (lang Java 1.8) Thanks!

public void print() {
    for (int i = 0; i < arr.length; i++) {
        if (i<=9) {
            System.out.print(arr[i] + " ");
        }else {
            System.out.print("\r");
        }
    }
}

Sample output

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 
etc....

You can see it from 2 differents point of view

  1. Each 10 numbers, print a new line : when the index ends with a 9 you reach ten elements so you print a new line println()

     public void print() { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); if (i % 10 == 9) { System.out.println(); } } } 
  2. Print enough number of line and on each one : print 10 elements

     public void print() { for (int i = 0; i < arr.length / 10; i++) { for (int j = 0; j < 10; j++) { System.out.print(arr[i * 10 + j] + " "); } System.out.println(); } } 

Code for any number of elements per line:

public void print(int elementsPerLine) {
  for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i]);
    if (i % elementsPerLine == 0 && i > 0) {
         System.out.println();
    } else {
        System.out.print(" ");
    }
  }
}

Use the following code,

 public static void printResult(int[][] result)
{
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<10; j++)
        {
            System.out.print(result[i][j] + ", ");
        }
        System.out.println();
    }
}

public static int[][] modifyArray( int[] singleArray )
{
    int columns = 10;
    int rows = singleArray.length/10;
    int[][] result = new int[rows][columns];

    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<columns; j++)
        {
            result[i][j] = singleArray[columns*i + j];
        }
    }
    return result;
}


public static void main(String[] args)
{
    int[] singleArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};

    int[][] result = modifyArray( singleArray);
    printResult( result );

}

You must use the modulo operator https://en.wikipedia.org/wiki/Modulo_operation

public void print() {
    for (int i = 0; i < arr.length; i++) {
        if (i%10 == 0) {
            System.out.print("\r");
        }else {
            System.out.print(arr[i] + " ");
        }
    }
}

Maybe you could write something like

if (i % 10 == 0)
    System.out.print("\r");
}
System.out.print(arr[i] + " ");

Take slices of 10 items into a new array each time and print that array:

int count = 10;
int iterations = (arr.length / count) +  ((arr.length % count) > 0 ? 1 : 0);
for (int i = 1; i <= iterations; i++) {
    int[] slice = new int[count];
    System.arraycopy(arr, (i - 1) * count, slice, 0, i == iterations ? (array.length % count) : count);
    System.out.println(Arrays.toString(slice));
}

The above code works for any value of count .

Take a look at a code down below:

   public class PrintEach10thElements {

    /**
     * @param args the command line arguments
     */
    static List<Integer> arrays = new ArrayList<>();

    public static void main(String[] args) {

        //create 50 elements in an array
        for (int i = 0; i <= 49; i++) {
            arrays.add(i);
        }

        for (int g = 0; g < arrays.size(); g++) {
            if ( arrays.get(g) % 10 != 0) {
                System.out.print(arrays.get(g) + " ");
            } else {
                System.out.println();
                System.out.print(arrays.get(g) + " ");

            }
        }

    }
}

If you don't mind using a Guava library, you can also do this

List<Integer> myList = Arrays.asList(myArray);
System.out.println(Lists.partition(myList, 10).stream()
        .map(subList -> subList.stream()
                               .map( Object::toString )
                               .collect(Collectors.joining(" ")))
        .collect(Collectors.joining("\n")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM