Is there a way to reverse specific arrays in a multidimensional array in java?
Asked Answered
N

5

0

I know how to generally manipulate and create a multidimensional array but I don't know all the utils and features that arrays have. I want to know is if I have a 2D array the size of [5][4], can I print it where the first line is in order, second is in reverse, and the third is in order... and so on.

For example:

[1 2 3 4] //in order
[8 7 6 5] //reverse
[9 10 11 12] //in order
[16 15 14 13] //reverse
[17 18 19 20] //in order

as my teacher stated "Define a two-dimensional array of size m × n. Write a method to initialize this array with numbers from 1 to m × n in the way as below: the first row, initialize the elements from left to right; the second row, initialize from right to left; then switch order. For example, if m=5; and n = 4; the array should be initialized to:"

I’m not sure if it should be done using a temp method or some other loop method.

Nectarous answered 20/12, 2020 at 18:17 Comment(0)
D
1

You cannot reverse it directly. But you can have a loop and reverse the alternative rows:

void reverseArray() {
    Integer[][] arr = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16},
            {17, 18, 19, 20}};
    for (int i = 1; i < arr.length; i += 2) {
        Collections.reverse(Arrays.asList(arr[i]));
    }
}
Demit answered 20/12, 2020 at 18:44 Comment(0)
M
0
int[][] arr = new int[][]{
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}};

AtomicInteger counter = new AtomicInteger(0);
Arrays.stream(arr).forEach(ints -> {
    System.out.println(Arrays.stream(ints)
            .mapToObj(String::valueOf)
            .reduce((a, b) ->
                    counter.get() % 2 == 0 ? a + " " + b : b + " " + a).get());
    counter.incrementAndGet();
});

This code uses the Stream API to iterate over an array. The first stream iterates over single-level arrays, the second - their elements, and then forms a string. Also, according to the counter value, items are combined from left to right or from right to left.

Mauk answered 20/12, 2020 at 18:47 Comment(0)
T
0

You can create such an array with a "snake order" without sorting at all, using a stream in a stream or a loop in a loop:


  1. int m = 5;
    int n = 4;
    
    int[][] arr = IntStream
            // create rows of array
            .range(0, m).mapToObj(row -> IntStream
                    // for each row create cells where
                    // values are numbers from 1 to [m * n]
                    .range(0, n).map(cell -> {
                        int val = row * n;
                        if (row % 2 == 0)
                            // even rows:
                            // straight order
                            val += cell + 1;
                        else
                            // odd rows:
                            // reverse order
                            val += n - cell;
                        return val;
                    })
                    // return int[] array
                    .toArray())
            // return int[][] 2d array
            .toArray(int[][]::new);
    

  1. int m = 5;
    int n = 4;
    
    int[][] arr = new int[m][n];
    
    // create rows of array
    for (int row = 0; row < m; row++) {
        // for each row create cells where
        // values are numbers from 1 to [m * n]
        for (int cell = 0; cell < n; cell++) {
            int val = row * n;
            if (row % 2 == 0)
                // even rows:
                // straight order
                val += cell + 1;
            else
                // odd rows:
                // reverse order
                val += n - cell;
            arr[row][cell] = val;
        }
    }
    

Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
// [1, 2, 3, 4]
// [8, 7, 6, 5]
// [9, 10, 11, 12]
// [16, 15, 14, 13]
// [17, 18, 19, 20]

See also:
How do I rotate a matrix 90 degrees counterclockwise in java?
Is there any other way to remove all whitespaces in a string?

Tracheid answered 20/12, 2020 at 19:32 Comment(0)
K
0

if I have a 2D array the size of [5][4], can I print it where the first line is in order, second is in reverse, and the third is in order... and so on.

It's unclear how you want to use the output, but here is a literal way to do it:

public static void main(String[] args) {
    int[][] values = new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
    };

    for (int r = 0; r < values.length; r++) {
        if (r % 2 == 0) {
            // forwards
            for (int c = 0; c < (values[r].length - 1); c++) {
                System.out.print(values[r][c] + " ");
            }
            System.out.println(values[r][values[r].length - 1]);
        } else {
            // backwards
            for (int c = (values[r].length - 1); c > 0; c--) {
                System.out.print(values[r][c] + " ");
            }
            System.out.println(values[r][0]);
        }
    }
}

Output:

1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Kusin answered 21/12, 2020 at 0:47 Comment(0)
P
0

Not as efficient as nested loops, one can simply iterator from 1 to 20 and determine row i and column j.

final int M = 5;
final int N = 4;
int[][] matrix = new int[M][N];

IntStream.range(0, M*N)
    .forEach(no -> { // no = 0, 1, 2, ... , M*N-1
        int i = no / N; // Row.
        int j = no % N; // Increasing column (for even row).
        if (i % 2 == 1) { // Odd row.
           j = N - 1 - j; // Decreasing column.
        }
        matrix[i][j] = no + 1;
    });

i % 2 is the modulo 2, rest by division of 2, hence 0 for even, 1 for odd.

Or use a bit more language features:

IntStream.range(0, N)
    .forEach(i -> {
        int no = N * i;
        IntUnaryOperator jToValue = i % 2 == 0
            ? j -> no + 1 + j
            : j -> no + N - 1 -j;
        Arrays.setAll(matrix[i], jToValue);
    });

Here Arrays.setAll(int[], (int index) -> int) fills the array based on the index.

About the question of there being some nice function:

You probably saw List.reverse; there does not exist an Arrays.reverse, hence Arrays.setAll seems to be best. In this case where the values are increasing one theoretically could also sort all odd rows reversed. But only with a trick, and sorting costs.

It is interesting that there are so many solutions. Instead of waggling the dog's tail one can take the tail and waggle the dog.

Phenica answered 24/12, 2020 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.