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.

3
  • 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]] Commented Apr 8, 2015 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? Commented Apr 8, 2015 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]] Commented Apr 8, 2015 at 20:20

4 Answers 4

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! :)

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know if i like Java8 :-)
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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.