获取有序二维数组的行列位置

huangapple go评论65阅读模式
英文:

Get the row and column position of a ordered 2D array

问题

// Java代码部分不要翻译,以下只是翻译后的内容。

我正在Java项目中工作需要按照二维数组中的值进行非递减排序并找到排序后元素的位置行和列)。我将尝试用一个示例来解释这个问题

以一个4x4的数组为例当然行和列的数量可以不同):
```java
array = [[2,0,36,90],[128,39,168,159],[44,9,105,193],[150,245,256,305]]

我将这个数组转换成了一个包含16个(4x4)元素的向量,其中每个元素都按照非递减顺序排列:

ordered_vector = [0,2,9,36,39,44,90,105,128,150,159,168,193,245,256,305]

现在我需要构建一个ordered_array,其中每个元素表示按非递减顺序排列的"array"中元素的行和列位置。例如,ordered_vector的第一个位置是0,对应于"array"中的第0行第1列;ordered_vector的第二个位置是2,对应于"array"中的第0行第0列;ordered_vector的第三个位置是9,对应于"array"中的第2行第1列;依此类推。

完整的输出应该如下所示:

ordered_array = {{0,1},{0,0},{2,1},{0,2},{1,1},{2,0},{0,3},{2,2},
                 {1,0},{3,0},{1,3},{1,2},{2,3},{3,1},{3,2},{3,3}};

如果对这个问题能够得到一些帮助,我将不胜感激。


<details>
<summary>英文:</summary>

I am working on a project in Java where I need to order a 2D array according to their values in non-decreasing order and find the position (row and column) after ordering their elements. I will try to explain this with an example.

Let us consider as an example an array of 4x4 (obviously the numbers of rows and columns may vary):

array = [[2,0,36,90],[128,39,168,159],[44,9,105,193],[150,245,256,305]]

I had transformed this array in a vector of 16 (4x4) elements where each element is ordered in non-decreasing order:

ordered_vector = [0,2,9,36,39,44,90,105,128,150,159,168,193,245,256,305]

Now that I need is to build an *ordered_array* where each element represents the row and column position of the elements of &quot;array&quot; ordered in non-decreasing order. For example, the first position of *ordered_vector* is 0 that corresponds to row 0 column 1 in array; the second position of *ordered_vector* is 2 that corresponds to row 0 column 0 in array; the third position of *ordered_vector* is 9 that corresponds to row 2 column 1 in array; and so on.

The complete output must be the following:

ordered_array = {{0,1},{0,0},{2,1},{0,2},{1,1},{2,0},{0,3},{2,2},
{1,0},{3,0},{1,3},{1,2},{2,3},{3,1},{3,2},{3,3}};

Would be highly appreciated if I can get some help with this question. 

</details>


# 答案1
**得分**: 1

以下是您提供的代码的翻译结果:

CustomArrayElement.java
```java
public class CustomArrayElement {
    int element;
    int row;
    int column;

    public CustomArrayElement(int element, int row, int column) {
        this.element = element;
        this.row = row;
        this.column = column;
    }

    public String toString() {
        return "{" + this.row + "," + this.column + "}";
    }
}

SortByElement.java

import java.util.Comparator;

public class SortByElement implements Comparator<CustomArrayElement> {
    @Override
    public int compare(CustomArrayElement o1, CustomArrayElement o2) {
        return o1.element - o2.element;
    }
}

OrderArrayClient.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OrderArrayClient {
    public static void main(String[] args) {
        int array[][] = {{2, 0, 36, 90}, {128, 39, 105, 159},
                {44, 9, 105, 193}, {150, 245, 256, 305}};
        int row = array.length;
        int column = array[0].length;
        CustomArrayElement customArrayElement = null;
        List<CustomArrayElement> listElements =
                new ArrayList<CustomArrayElement>(row * column);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                customArrayElement =
                        new CustomArrayElement(array[i][j], i, j);
                listElements.add(customArrayElement);
            }
        }

        Collections.sort(listElements, new SortByElement());
        int[][] output = new int[listElements.size()][2];
        int j = 0;
        for (int i = 0; i < listElements.size(); i++) {
            if (i % 2 <= 1) {
                output[i][j] = listElements.get(i).row;
                output[i][j + 1] = listElements.get(i).column;
                j = 0;
            }
        }

        for (int i = 0; i < output.length; i++) {
            for (int k = 0; k < 2; k++) {
                if (k == 0) {
                    System.out.print("{" + output[i][k] + ",");
                } else {
                    System.out.print(output[i][k] + "}");
                }
            }
            System.out.println();
        }
    }
}

输出结果:

{0,1}
{0,0}
{2,1}
{0,2}
{1,1}
{2,0}
{0,3}
{1,2}
{2,2}
{1,0}
{3,0}
{1,3}
{2,3}
{3,1}
{3,2}
{3,3}
英文:

Can you please check if this code work for you?

CustomArrayElement.java

public class CustomArrayElement {
    int element;
    int row;
    int column;

    public CustomArrayElement(int element, int row, int column) {
        this.element = element;
        this.row = row;
        this.column = column;
    }

    public String toString() {
        return &quot;{&quot; + this.row + &quot;,&quot; + this.column + &quot;}&quot;;
    }
}

SortByElement.java

import java.util.Comparator;

public class SortByElement implements Comparator&lt;CustomArrayElement&gt; {
    @Override
    public int compare(CustomArrayElement o1, CustomArrayElement o2) {
        return o1.element - o2.element;
    }
}

OrderArrayClient.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OrderArrayClient {
    public static void main(String[] args) {
        int array[][] = {{2, 0, 36, 90}, {128, 39, 105, 159},
                {44, 9, 105, 193}, {150, 245, 256, 305}};
        int row = array.length;
        int column = array[0].length;
        CustomArrayElement customArrayElement = null;
        List&lt;CustomArrayElement&gt; listElements =
                new ArrayList&lt;CustomArrayElement&gt;(row * column);
        for (int i = 0; i &lt; row; i++) {
            for (int j = 0; j &lt; column; j++) {
                customArrayElement =
                        new CustomArrayElement(array[i][j], i, j);
                listElements.add(customArrayElement);
            }
        }

        Collections.sort(listElements, new SortByElement());
        int[][] output = new int[listElements.size()][2];
        int j = 0;
        for (int i = 0; i &lt; listElements.size(); i++) {
            if (i % 2 &lt;= 1) {
                output[i][j] = listElements.get(i).row;
                output[i][j + 1] = listElements.get(i).column;
                j = 0;
            }
        }

        for (int i = 0; i &lt; output.length; i++) {
            for (int k = 0; k &lt; 2; k++) {
                if (k == 0) {
                    System.out.print(&quot;{&quot; + output[i][k] + &quot;,&quot;);
                } else {
                    System.out.print(output[i][k] + &quot;}&quot;);
                }
            }
            System.out.println();
        }
    }
}

Output:

{0,1}
{0,0}
{2,1}
{0,2}
{1,1}
{2,0}
{0,3}
{1,2}
{2,2}
{1,0}
{3,0}
{1,3}
{2,3}
{3,1}
{3,2}
{3,3}

答案2

得分: 1

你可以首先准备一系列的二维数组索引对,然后使用comparingInt方法对该序列进行排序,排序依据是对应数组的值:

int[][] arr = {
        {2, 0, 36, 90},
        {128, 39, 168, 159},
        {44, 9, 105, 193},
        {150, 245, 256, 305}};
int[][] arr2 = IntStream.range(0, arr.length)
        .mapToObj(i -> IntStream.range(0, arr[i].length)
                // 二维数组索引对
                .mapToObj(j -> new int[]{i, j})
                .toArray(int[][]::new))
        .flatMap(Arrays::stream)
        // 按照对应的数组值进行排序
        .sorted(Comparator.comparingInt(pair -> arr[pair[0]][pair[1]]))
        .toArray(int[][]::new);
// 输出
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
[0, 1]
[0, 0]
[2, 1]
[0, 2]
[1, 1]
[2, 0]
[0, 3]
[2, 2]
[1, 0]
[3, 0]
[1, 3]
[1, 2]
[2, 3]
[3, 1]
[3, 2]
[3, 3]
英文:

You can first prepare a sequence of pairs of 2d array indexes and then sort that sequence by the corresponding array values using the
comparingInt method:

int[][] arr = {
        {2, 0, 36, 90},
        {128, 39, 168, 159},
        {44, 9, 105, 193},
        {150, 245, 256, 305}};
int[][] arr2 = IntStream.range(0, arr.length)
        .mapToObj(i -&gt; IntStream.range(0, arr[i].length)
                // pair of 2d array indexes
                .mapToObj(j -&gt; new int[]{i, j})
                .toArray(int[][]::new))
        .flatMap(Arrays::stream)
        // sort by the corresponding array values
        .sorted(Comparator.comparingInt(pair -&gt; arr[pair[0]][pair[1]]))
        .toArray(int[][]::new);
// output
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
[0, 1]
[0, 0]
[2, 1]
[0, 2]
[1, 1]
[2, 0]
[0, 3]
[2, 2]
[1, 0]
[3, 0]
[1, 3]
[1, 2]
[2, 3]
[3, 1]
[3, 2]
[3, 3]

huangapple
  • 本文由 发表于 2020年9月21日 23:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定