简体   繁体   English

从输入的一维数组返回一个二维数组

[英]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 .我想采用1x6的输入 1d 数组并将其返回到3x2的 2d 数组中。 I think I have figured it out but in my code I keep getting this error array required, but int found.我想我已经弄清楚了,但是在我的代码中,我一直需要这个错误数组,但找到了int 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返回一个 2D 数组3x2

Output: 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.您的第一组循环需要与第二组循环相同,否则transmo[i][j]将失败。

You then need to calculate the index into d .然后,您需要将索引计算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:迭代一维数组,然后使用模数和余数索引二维 output 数组:

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].由于 d[] 是一维数组,您应该使用 d[index] 访问它。 Here what you need to do is resolve the relationship between the index of tansmo and the index of d这里你需要做的是解决tansmo的索引和d的索引之间的关系

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

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另请参阅:如何对预定义数组进行平方并在 3 个 integer 空间中打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM