简体   繁体   English

获取Java中Matrix的排序值

[英]Get sorted values of Matrix in Java

I'm totally new in Java. 我是Java的新手。 I need to "sort" the value of a matrix in Java but I don't know how to do. 我需要对Java中矩阵的值进行“排序”,但我不知道该怎么做。

This is my matrix: 这是我的矩阵:

double matrix = new double[3][3];
// fill matrix with some random values

The result of the matrix could be something like this: 矩阵的结果可能是这样的:

3,4    9.4    7.7
4,1    3.48   9.9
1,6    3.5    5.3

Now I need to get the i & j value of this matrix in decrease order of the corresponding value. 现在,我需要按相应值的降序获得此矩阵的i&j值。 There are some way to do that? 有一些方法可以做到吗?

In this case the vector with the sorted value it should contain something like this: (1,2), (0,1), (0,2), (2,2) ... (2,1) 在这种情况下,具有排序值的向量应包含以下内容:(1,2),(0,1),(0,2),(2,2)...(2,1)

With Java 8 you can do this with Lambda: 使用Java 8,您可以使用Lambda做到这一点:

int rows = matrix.lenght;
int cols = matix[0].lenght;

List<List<Integer>>orderedSavingsList = IntStream.range(0, rows).mapToObj(i ->
            IntStream.range(0, cols).mapToObj(j ->
                    new double[]{i, j, matrix[i][j]}
            )
    )
    .flatMap(x -> x).filter(t -> t[0] < t[1])
    .sorted((a, b) -> Double.compare(b[2], a[2]))
    .map(a -> Arrays.asList((int) a[0], (int) a[1]))
    .collect(Collectors.toList());

After that try to print the list with: 之后,尝试使用以下命令打印列表:

orderedSavingsList.forEach(System.out::println);

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

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