简体   繁体   English

如何在 Java 中转换二维整数数组中的有序对数组

[英]How to transform in Java an array of ordered pairs in a 2D array of integers

I have a 2D array in Java of 50 rows and 2 columns:我在 Java 中有一个 50 行和 2 列的二维数组:

int[][] listOne = new int [][] {{2,1},{8,0},{1,1},{4,0},{8,1},
                                       {8,3},{5,1},{2,0},{8,2},{2,3},
                                       {0,0},{4,1},{2,2},{8,4},{5,0},
                                       {2,4},{6,1},{5,3},{1,0},{4,2},
                                       {7,0},{5,2},{0,1},{1,3},{5,4},
                                       {0,2},{3,0},{7,1},{0,3},{7,2},
                                       {9,0},{0,4},{4,3},{3,1},{3,2},
                                       {6,0},{7,3},{9,1},{4,4},{6,3},
                                       {9,2},{7,4},{9,3},{6,2},{9,4},
                                       {6,4},{1,2},{3,3},{1,4},{3,4}};

I need to "clone" this array in other 2D array of 10 rows and 5 columns of integers where each number represent the position of the ordered pairs in listOne.我需要在其他 10 行和 5 列整数的二维数组中“克隆”这个数组,其中每个数字代表 listOne 中有序对的位置。

For example, listTwo[2][1]=1 (please note than "[2]" is listOne[0][0] and "[1]" is listOne[0][1]), listTwo[8][0]=2 (where "[8]" is listOne[1][0] and "[0]" is listOne[1][1]), and so on.例如,listTwo[2][1]=1(请注意“[2]”是listOne[0][0],“[1]”是listOne[0][1]),listTwo[8][ 0]=2(其中“[8]”是listOne[1][0],“[0]”是listOne[1][1]),依此类推。

Any help would be highly appreciated.任何帮助将不胜感激。

I have tried the following code:我尝试了以下代码:

import java.util.Arrays;

public class Test {

static int rows = 10;
static int columns = 5;
static int[][] listOne = new int [][] {{2,1},{8,0},{1,1},{4,0},{8,1},
                                       {8,3},{5,1},{2,0},{8,2},{2,3},
                                       {0,0},{4,1},{2,2},{8,4},{5,0},
                                       {2,4},{6,1},{5,3},{1,0},{4,2},
                                       {7,0},{5,2},{0,1},{1,3},{5,4},
                                       {0,2},{3,0},{7,1},{0,3},{7,2},
                                       {9,0},{0,4},{4,3},{3,1},{3,2},
                                       {6,0},{7,3},{9,1},{4,4},{6,3},
                                       {9,2},{7,4},{9,3},{6,2},{9,4},
                                       {6,4},{1,2},{3,3},{1,4},{3,4}};

static int[][] listTwo = new int [rows][columns];

public static void main(String[] args) {

    for (int k = 0; k < listOne.length; k++) {

    // some code here

    }

System.out.println("listTwo " +Arrays.deepToString(listTwo));

  // the result must be an array of 10 rows and 5 columns:

  // {11,23,26,29,32}      
  // {19,3,47,24,49}
  // {8,1,13,10,16}
  // {27,34,35,48,50}
  // {4,12,20,33,39}
  // {15,7,22,18,25}
  // {36,17,44,40,46}
  // {21,28,30,37,42}
  // {2,5,9,6,14}
  // {31,38,41,43,45}

  }
}

The result must be an array of 10 rows and 5 columns:结果必须是一个 10 行 5 列的数组:

  {11,23,26,29,32}      
  {19,3,47,24,49}
  {8,1,13,10,16}
  {27,34,35,48,50}
  {4,12,20,33,39}
  {15,7,22,18,25}
  {36,17,44,40,46}
  {21,28,30,37,42}
  {2,5,9,6,14}
  {31,38,41,43,45}

Or could be:或者可以是:

  {10,22,25,28,31} .... and so on if we begin the count in 0.

You need to iterate over listOne to retrieve the single D arrays.您需要遍历 listOne 以检索单个 D 数组。 Then use the values to index into listTwo and set the position.然后使用这些值索引到 listTwo 并设置位置。

      int pos = 1;
      int[][] listTwo = new int[10][5];
      for (int r = 0; r < listOne.length; r++) {
         int[] v = listOne[r];
         listTwo[v[0]][v[1]] = pos++;
      }

Use the following to print out the results.使用以下方法打印结果。

      for (int[] t : listTwo) {
         System.out.println(Arrays.toString(t));
      }

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

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