【问题标题】:How to sort a 2 dimensional array with all elements in ascending order in java如何在java中对所有元素按升序排列的二维数组进行排序
【发布时间】:2017-06-18 09:50:46
【问题描述】:

有没有像使用Arrays.sort()一样在java中对二维数组进行排序的有效方法。例如:-

a={{3 ,1},{0,2})
result={{0,1},{2,3}}

这是我的方法:

  for (int x = 0; x < n; x++) {
      for (int y = 0; y < n; y++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] > grid[x][y]) {
                        int t = grid[x][y];
                        grid[x][y] = grid[i][j];
                        grid[i][j] = t;
                    }
                }
            }
        }
    }

【问题讨论】:

  • 鉴于您的数组丢失零并获得四,我认为这不算作排序。
  • @JoeC..抱歉输入错误
  • @Aominè 该帖子处理按列排序.​​.我不想只对列进行排序..但整个数组
  • 看来你只需要一个一维数组,然后成对地对元素进行分组。
  • 然后创建一个包含二维数组所有元素的一维数组,对其进行排序,然后从排序后的一维数组创建一个二维数组。

标签: java arrays


【解决方案1】:

可以先对所有元素进行排序,然后生成pair或者n元素,例如;

int[][] a={{3 ,1},{0,2}};
int count = a[0].length;

//Sort the elements
List<Integer> sortedElements = Arrays.stream(a)
    .flatMapToInt(e -> Arrays.stream(e))
    .boxed()
    .sorted()
    .collect(Collectors.toList());

//Now, generate 2D arrays
int[][] arrays = new int[a.length][];
int[] temp = new int[count];
int index = 0;
for(int i = 0; i < sortedElements.size() ; i++){
    if(i % count == 0 && i != 0){
        arrays[index++] = temp;
        temp = new int[count];
    }
    temp[i % count] = sortedElements.get(i);
}
arrays[index++] = temp;

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

【讨论】:

【解决方案2】:

您可以使用以下方法解决您的问题:

public static void main(String[] args) {
    Integer[][] a = {{3, 1}, {0, 2}};
    List<Integer> list = new ArrayList<>();
    for (Integer[] i : a) {//<--convert the 2d to list-------------------------(1)
        list.addAll(Arrays.asList(i));
    }
    Collections.sort(list);//<--sort this list---------------------------------(2)
    Integer[][] result = new Integer[a.length][];//create new 2d array---------(3)
    int k = 0;
    for (int i = 0; i < a.length; i++) {//loop throw the original array--------(4) 
        //creae temp array with the same size of each 1d array-----------------(5)
        Integer[] temp = new Integer[a[i].length];
        //loop and fill temp array with elements of the list-------------------(6)
        for (int j = 0; j < a[i].length; j++) {
            temp[j] = list.get(k);
            k++;
        }
        result[i] = temp;//add the temp to the new array-----------------------(7)
    }

    System.out.println(Arrays.deepToString(result));//print the new array
}

例子

input                                           output
{{3, 1}, {0, 2}}                                [[0, 1], [2, 3]]
{{3, 1, 5}, {0, 2}}                             [[0, 1, 2], [3, 5]]     
{{3, 1, 5}, {0, 5}, {3, 7, 5}, {10, 9, 11}}     [[0, 1, 3], [3, 5], [5, 5, 7], [9, 10, 11]]

注意此方案将保证原始数组的每个节点的长度相同。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2021-12-06
  • 2019-09-09
  • 2020-10-05
  • 1970-01-01
  • 2016-02-17
相关资源
最近更新 更多