4

Suppose, I have an array like

[1,2,3,4,5,6,7,8,9,10,11,12]

Are there any standard methods to transform it to the table with N columns?

For example, if N=3:

[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

or if N=4:

[[1,2,3,4],[5,6,7,8],[9,10,11,12]]

etc.

TomCho
  • 3,204
  • 6
  • 32
  • 83
Alexei Vinogradov
  • 1,548
  • 3
  • 15
  • 34
  • If `N=5` in your example, would I be correct in answering as `[[1,2,3,4,5],[6,7,8,9,10],[11,12]]` – DoubleDouble Apr 08 '15 at 19:42
  • I don't think this is of enough general use that there would be libraries, but it should be pretty straightforward to do? – Giovanni Botta Apr 08 '15 at 19:45
  • @DoubleDouble in my case it is always a table, but if not, probably filling 0 for primitives and null for Objects to be full: [[1,2,3,4,5],[6,7,8,9,10],[11,12,0,0,0]] – Alexei Vinogradov Apr 08 '15 at 20:20

4 Answers4

2

With Java 8, you can use an IntStream to generate the corresponding indexes that you'll give to Arrays.copyOfRange.

I answered a sort of a similar question and you can find the logic there but here's it's slightly modified to take the array as parameter:

static List<int[]> partitionIntoList(int[] arr, int pageSize) {
    return IntStream.range(0, (arr.length + pageSize - 1) / pageSize)
        .mapToObj(i -> Arrays.copyOfRange(arr, i * pageSize, min(pageSize * (i + 1), arr.length)))
        .collect(toList());
}

static int[][] partitionIntoArray(int[] arr, int pageSize) {
    return IntStream.range(0, (arr.length + pageSize - 1) / pageSize)
        .mapToObj(i -> Arrays.copyOfRange(arr, i * pageSize, min(pageSize * (i + 1), arr.length)))
        .toArray(int[][]::new);
}

Note that if pageSize does not partition perfectly the input's size, the remaining elements are added in the last int array.

For example,

partitionIntoArray(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 4);

outputs:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

and if you take a page size of 5, the two last elements will be added to a third array:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12]]

Hope it helps! :)

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

We could do it this way :

int n = 5;  // what ever size you want to break it with
int[] bytes = {1,2,3,4,5,6,7,8,9,10,11,12};
int length = bytes.length;
int counter = 0;
int newSize = length % n == 0 ? length/n : (length/n)+1;
int[][] newArray = new int[newSize][n];
for (int i = 0; i < length - n + 1; i += n)
    newArray[counter++] = Arrays.copyOfRange(bytes, i, i + n);

if (length % n != 0)
    newArray[counter] = Arrays.copyOfRange(bytes, length - length % n, length);
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
1

Just using for loops:

    int[] array = {1,2,3,4,5,6,7,8,9,10,11,12};
    int n = 3;
    int m = (int) Math.ceil( ( (double) array.length ) / n );
    int[][] table = new int[m][n];
    int k = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (k < array.length) {
                table[i][j] = array[k];
                k++;
            }
        }
    }

The output should be what you are asking for.

Avalanche
  • 386
  • 2
  • 10
1

Not really but you can create one easily using some math and a loop:

public int[][] getTable(int[] arr, int n) {
    int[][] table = new int[(int)Math.ceil(arr.length / (float)n)][n];
    for (int i = 0, row = 0, column = 0; i < arr.length; i++) {
        if (i % n == 0 && i != 0) {
            row++;
            column = 0;
        }
        table[row][column++] = arr[i];
    }
    return table;
}
Titus
  • 22,031
  • 1
  • 23
  • 33