簡體   English   中英

如何在行中打破一維數組?

[英]How to break a one-dimensional array in rows?

我想在行中打破一維數組。 數組尺寸為50。我需要將數組輸出到控制台,每行10個元素。 (lang Java 1.8)謝謝!

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

樣品輸出

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

您可以從2個不同的角度來看它

  1. 每10個數字打印一個新行:當索引以9結尾時,您將到達10個元素,因此您將打印一個新行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. 打印足夠的行數,每行上:打印10個元素

     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(); } } 

每行任意數量的元素的代碼:

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(" ");
    }
  }
}

使用以下代碼,

 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 );

}

您必須使用模運算符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] + " ");
        }
    }
}

也許你可以寫一些像

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

每次將10個項目的切片切成一個新數組並打印該數組:

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));
}

上面的代碼適用於任何count值。

看看下面的代碼:

   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) + " ");

            }
        }

    }
}

如果您不介意使用Guava庫,也可以執行此操作

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")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM