简体   繁体   中英

Returning a 2d array from inputed 1d array

I want to take the input 1d array that is a 1x6 and return it into a 2d array that is a 3x2 . I think I have figured it out but in my code I keep getting this error array required, but int found. I think this is due to the input array being a 1d array and not a 2d, but I'm not sure. Could anyone help me to fix this?

Example:

int[] d = {4, 1, 20, 45, 2, 31};

return a 2D array 3x2

Output:

4  1  
20 45  
2   31

Error:

transmo[j][i] = d[i][j];
array required, but int found

My code below:

public class mypractice {
    public void a_three_by_two(int[] d) {
        int transmo[][] = new int[3][2];

        for (int i = 0; i < 1; i++) {
            for (int j = 0; j < 6; j++) {
                transmo[j][i] = d[i][j];
            }
        }

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(transmo[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Your first set of loops need to be the same as the second set of loops, otherwise transmo[i][j] will fail.

You then need to calculate the index into d .

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        transmo[i][j] = d[i * 2 + j];
    }
}

Iterate the 1D array and then index the 2D output array using the modulus and remainder:

public int[][] a_three_by_two(int[] d, int nrow, int ncol) {
    int transmo[][] = new int[nrow][ncol];
    
    for (int i=0; i < d.length; ++i) {
        int row = i / ncol;
        int col = i % ncol;
        transmo[row][col] = d[i];
    }

    return transmo;
}

int[] d = {4, 1, 20, 45, 2, 31};
int[][] output = a_three_by_two(d, 2, 3);
System.out.println(Arrays.deepToString(output));

This prints:

[[4, 1], [20, 45], [2, 31]]

The logic here is that for every two steps along the input 1D array, we increase the row index by one, and we increase the column index based on the remainder after dividing by two.

Note that this version of your code is flexible and works for any dimensional 2D array.

Since d[] is a 1d array you should access it by using d[index]. Here what you need to do is resolve the relationship between the index of tansmo and the index of d

  • i = 0 j = 0 => d[index] index = 0
  • i = 0 j = 1 => d[index] index = 1
  • i = 1 j = 0 => d[index] index = 2
  • i = 1 j = 1 => d[index] index = 3
  • i = 2 j = 0 => d[index] index = 4
  • i = 2 j = 1 => d[index] index = 5
  • 2*i+j = index

So your loop gonna be like this:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++){
        transmo[i][j] = d[2*i+j];
    }
}

You can use streams to create and populate a new 2d array from a 1d array:

int[] arr1d = {4, 1, 20, 45, 2, 31};
// index of the element in the 1d array
AtomicInteger i = new AtomicInteger(0);
// create and populate a new 2d array '3x2'
int[][] arr2d = IntStream.range(0, 3)
        .mapToObj(row -> IntStream.range(0, 2)
                // take the element from the 1d
                // array and increment the index
                .map(j -> arr1d[i.getAndIncrement()])
                .toArray())
        .toArray(int[][]::new);
// output
System.out.println(Arrays.deepToString(arr2d));
// [[4, 1], [20, 45], [2, 31]]

See also: How do I square a predefined array and print it in 3 integer spaces

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