简体   繁体   English

将矩阵中的排序合并到 Java 中的列表中

[英]Merge sort from matrix into list in Java

I'm wondering how it's possible to merge sort a matrix into a list?我想知道如何将矩阵合并到列表中?

public class MergeRows {
public static void main(String[] args) {
    int matrix[][] = {
        {1, 3, 5},
        {1, 2, 6},
        {4, 7, 8}
    };
    mergeSort(matrix);
}

public static int mergeSort(int[][] matrix) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            list.addAll(Arrays.asList(matrix));
        }     

        Collections.sort(list);
        System.out.println();
    }
}

I'm quite stuck on this one.我很坚持这个。 It must return [1,1,2,3,4,5,6,7,8].它必须返回 [1,1,2,3,4,5,6,7,8]。

You must delete the nested loop, matrix[i] is an array so can be passed into Arrays.asList()您必须删除嵌套循环, matrix[i]是一个数组,因此可以传递给Arrays.asList()

public static int mergeSort(int[][] matrix) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < matrix.length; i++) {       
      list.addAll(Arrays.asList(matrix[i]));
    }

    Collections.sort(list);
    System.out.println(Arrays.toString(list.toArray()));
}

The solution in Java is as follows: Java中的解决方法如下:

List<Integer> list = new List<Integer>();
for (int row = 0; row < matrix.Length; row++)
{
  for (int column = 0; column < matrix[row].Length; column++)
  {
    list.Add(matrix[row][column]);
  }
}

var result = Collections.sort(list);
System.out.println(String.join(",", result));

Two things to note from your solution that are different here.从您的解决方案中需要注意的两件事在这里有所不同。 First make sure you do the sort outside the for loops, as you would just be sorting the list each time you add an item and them messing the list up again.首先确保在 for 循环之外进行排序,因为每次添加项目时都会对列表进行排序,然后它们又将列表弄乱了。

Secondly, you were adding the whole matrix to the list each loop, this method will loop through both dimensions of the matrix and add each element to the new list.其次,您在每个循环中都将整个矩阵添加到列表中,此方法将遍历矩阵的两个维度并将每个元素添加到新列表中。

Hope this helps!希望这可以帮助!

Edit Ignore the following it's the C# version, leaving for reference incase.编辑 忽略以下是 C# 版本,留作参考。 Didn't see the question marked as Java!没有看到标记为Java的问题! Solution is for C#.解决方案适用于 C#。

There is a very quick one line solution to your problem using System.Linq however I shall try to explain it clearly in case you/or anyone viewing doesn't know Linq, forgive me if I'm explaining something you know.使用System.Linq有一个非常快速的单行解决方案可以解决您的问题,但是如果您/或查看的任何人不了解 Linq,我将尝试清楚地解释它,如果我正在解释您知道的内容,请原谅我。

var matrix = new int[][] {
    new int[] { 1, 3, 5 },
    new int[] { 1, 2, 6 },
    new int[] { 4, 7, 8 }
  };

var result = matrix.SelectMany(record => record).OrderBy(record => record).ToList();
Console.WriteLine(String.Join(", ", result));

produces the following in the console: 1, 1, 2, 3, 4, 5, 6, 7, 8在控制台中产生以下内容: 1, 1, 2, 3, 4, 5, 6, 7, 8

The reason for this is, SelectMany takes a 2d array ([][]) and flattens it into a single array.这样做的原因是, SelectMany接受一个二维数组 ([][]) 并将其展平为单个数组。 The record => record portion is simply the selector. record => record部分只是选择器。 In the same way OrderBy(record => record) simply sorts the result of the SelectMany .以同样的方式OrderBy(record => record)简单地对SelectMany的结果进行排序。

SelectMany(record => record) means that you cycle throw each of the arrays and select the array items. SelectMany(record => record)意味着您循环抛出每个数组并选择数组项。 For example, if you hand a 2d array as follows:例如,如果您按如下方式处理 2d 数组:

class Item {
   public string Name { get; set; }
   public string Value { get; set; }
}

var matrix = Item[][] { ... some items ... }

var result = matix.SelectMany(items => items)

Would return a result of Item[] with all the items in a single array from the matrix.将返回Item[]的结果,其中包含矩阵中单个数组中的所有项目。

You can take this further to matix.SelectMany(items => items.Select(item => item.Name)) would return a string[] with just the names from each item.您可以进一步使用matix.SelectMany(items => items.Select(item => item.Name))将返回一个string[]其中仅包含每个项目的名称。

I recommend looking at Linq, it's very useful for operations like this.我建议看一下 Linq,它对这样的操作非常有用。 But beware, you can go wild with it!但要小心,你可以疯狂地使用它!

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

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