Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据arr[i]之和从低到高对二维int数组进行排序_Java_Arrays_Sorting_Multidimensional Array - Fatal编程技术网

Java 根据arr[i]之和从低到高对二维int数组进行排序

Java 根据arr[i]之和从低到高对二维int数组进行排序,java,arrays,sorting,multidimensional-array,Java,Arrays,Sorting,Multidimensional Array,我的作业已经到了一个死胡同,那就是对二维数组中的整数进行排序 说明是创建一个接受二维int数组的函数 (不一定是矩阵)并根据内部数组的和对外部数组进行排序。换句话说,数组的第一个索引中应该是和最小的内部数组 示例- input-int[][]数组={{2,4,1,9,9,9},{6,8},{7,3,6,5,1} 输出数组={6,8},{7,3,6,5,1},{2,4,1,9,9,9} 到目前为止,我的逻辑是创建一个新的一维数组,该数组将包含和 来自主阵列的每个内部阵列的。 并根据它进行排序 pu

我的作业已经到了一个死胡同,那就是对二维数组中的整数进行排序

说明是创建一个接受二维int数组的函数 (不一定是矩阵)并根据内部数组的和对外部数组进行排序。换句话说,数组的第一个索引中应该是和最小的内部数组

示例- input-int[][]数组={{2,4,1,9,9,9},{6,8},{7,3,6,5,1}

输出数组={6,8},{7,3,6,5,1},{2,4,1,9,9,9}

到目前为止,我的逻辑是创建一个新的一维数组,该数组将包含和 来自主阵列的每个内部阵列的。 并根据它进行排序

public static int[] arraysCalculator(int[][] arr) { 
int[] sums = new int[arr.length];
int sum= 0;
for(int i = 0; i  < arr.length; i++)
{
    for(int j = 0; j < arr[i].length; j++)
    {
        sum += arr[i][j];
    }
    sums[i] = sum;
    sum = 0;
}
公共静态int[]arraysCalculator(int[]arr){
int[]总和=新的int[arr.length];
整数和=0;
对于(int i=0;i
您可以通过流式处理
int[]
然后调用
sum()
来轻松求和
int[]
。从这里开始,只需调用
数组。使用比较此和的比较器对
排序:

Arrays.sort(array, Comparator.comparingInt(a -> Arrays.stream(a).sum()));

你有三分之一。你现在需要的是:

  • 创建包含“内部”数组的和的第一个数组
    sums
    后,只需创建该数组的精确副本,如
    originalSums
  • 然后,对
    总和的内容进行排序
示例:假设
总和
原始数
[12,3,7]
。排序后,您会得到:

originalSums
[12,3,7]

总和
[3,7,12]

问题是:通过查看两个数组,您可以决定如何交换内部数组以匹配
现在显示的求和顺序

在上面的示例中,您注意到
12
的原始索引为0。排序后,其索引为2。因此您知道必须将内部数组“交换”为0和2。您还可以推断
3
应转到索引0,而
7
应转到索引2

当然,要确保交换成功还需要一些工作(比如最好不要交换任何索引两次)

让整个过程顺利进行的最基本的方法就是一次只查看一个索引(就像你做的某种冒泡排序)


长话短说:有许多不同的方法可以解决这个问题。我建议你从最简单的方法开始。

除了那些解释理论和为解决作业奠定基础的回答之外,这里有一个完整的工作示例(详细说明每件事的作用和原因)希望这会有所帮助:

/**
 * This method will sort the inner arrays of a given two dimensional array
 * from lowest to highest value according to the sum of it's elements.
 */
private static int[][] arrayCalc(int[][] arr)
{
    /*
     * We're working with a TreeMap here because this type
     * of map is allowed to have duplicate key entries
     */
    java.util.Map<Integer, Integer[]> map = new java.util.TreeMap<>();
    /*
     * This value represents the largest inner array
     * size found in 2d array passed as parameter
     */
    int largestSize = 0;
    for (int[] inner : arr) {
        /*
         * Convert the inner array to an array of Integer
         * objects so it can be placed inside a map
         */
        Integer[] integers = IntStream.of(inner).boxed().toArray(Integer[]::new);
        map.put(IntStream.of(inner).sum(), integers);
        /*
         * Check if this inner array has a larger value
         * then the largest array we processed so far
         */
        if (inner.length > largestSize) {
            largestSize = inner.length;
        }
    }

    int[][] result = new int[map.size()][largestSize];
    /*
     * Iterate over the map and copy it's values which are represented
     * as Integer arrays into inner arrays of our return value
     */
    java.util.Iterator<java.util.Map.Entry<Integer, Integer[]>> iter = map.entrySet().iterator();
    for (int i = 0; i < result.length && iter.hasNext(); i++)
    {
        java.util.Map.Entry<Integer, Integer[]> entry = iter.next();
        /*
         * We can just return our value as an array of Integer objects
         * but for the sake of this exercise we will convert it to a
         * primitive 2D int array so it's consistent with our method parameter
         */
        Integer[] integers = entry.getValue();
        result[i] = java.util.Arrays.stream(integers).mapToInt(Integer::intValue).toArray();
    }
    return result;
}

public static void main(String[] args)
{
    int[][] array = {{2, 4, 1,9,9,9,9}, {6, 8}, {7, 3, 6, 5, 1}};
    int[][] result = arrayCalc(array);

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

我不需要代码,我非常感谢人们纠正我的误解。而且,一旦我有了int[]数组中的每个索引都包含大数组中匹配的内部数组的总和。从这一点上说,我可以使用气泡排序来根据我的小数组对大数组进行排序。但是由于某种原因,我的代码不起作用,我相信我的逻辑是正确的。@AvivDavidMalka请看我的答案。我已经尽力为您提供了代码和e解释每个部分都做什么以及为什么做。所以,即使你没有要求任何代码,有时从工作示例中学习是最好的学习方式。
[6, 8]
[7, 3, 6, 5, 1]
[2, 4, 1, 9, 9, 9, 9]