繁体   English   中英

Java-按行总和对2D数组排序

[英]Java - Sorting a 2D Array by Row Sum

尝试编写一种以增加行总和的顺序交换2D数组的行的方法。

例如,如果我有以下2d数组:

int [][] array = {4,5,6},{3,4,5},{2,3,4};

我希望它这样输出一个数组:

{2 3 4}, {3 4 5}, {4 5 6}

方法:

a。)取每一行的总和,并制成总和的一维数组

b。)对rowSum数组进行冒泡排序

c。)根据进行的冒泡排序交换来交换原始数组的行

d。)然后打印新行排序的数组。

到目前为止,这是我的代码:

      public void sortedArrayByRowTot() {
        int [][] tempArray2 = new int [salaryArray.length [salaryArray[0].length];

        for (int i = 0; i < tempArray2.length; i++) {
          for (int j = 0; j < tempArray2[i].length; j++) {
            tempArray2[i][j] = salaryArray[i][j];
          }
        }

        int [] rowSums = new int [tempArray2.length];
        int sum = 0;
        for (int i = 0; i < tempArray2.length; i++) {
          for (int j = 0; j < tempArray2[i].length; j++) {
            sum += tempArray2[i][j];
          }
          rowSums[i] = sum;
          sum = 0;
        }

        int temp;
        int i = -1;
        for(int j = rowSums.length; j > 0; j--){
          boolean isSwap = false;
          for (i = 1; i < j; i++) {
            if(rowSums[i-1] > rowSums[i]) {
              temp = rowSums[i-1];
              rowSums[i-1] = rowSums[i];
              rowSums[i] = temp;
              isSwap = true;
            }
          }

          if(!isSwap){
            break;
          }
        }

        for (int k = 0; k < tempArray2.length; k++) {
          temp = tempArray2[i-1][k];
          tempArray2[i-1][k] = tempArray2[i][k];
          tempArray2[i][k] = temp;
        }

        for (int b = 0; b < tempArray2.length; b++) {
          for (int c = 0; c < tempArray2[b].length; c++) {
            System.out.print(tempArray2[b][c] + " ");
          }
        }
      }
    }

不确定我是否正确执行了方法的c部分?

它一直说“线程“ main”中的异常 java.lang.ArrayIndexOutOfBoundsException:2”

正如@shmosel所说,您可以这样操作:

public static void sortedArrayByRowTot() {
    int [][] array = {{4,5,6},{3,4,5},{2,3,4}};
    Arrays.sort(array, Comparator.comparingInt(a -> IntStream.of(a).sum()));
}

我能够解决我的问题。 谢谢。

    public void sortedArrayByRowTot() {
      //Creates tempArray2 to copy salaryArray into
      int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];

      //Copies salaryArray into tempArray2
      for (int i = 0; i < salaryArray.length; i++) {
        for (int j = 0; j < salaryArray[i].length; j++) {
          tempArray2[i][j] = salaryArray[i][j];
        }
      }

      //Creates rowSum array to store sum of each row
      int [] rowSums = new int [tempArray2.length];
      for (int i = 0; i < tempArray2.length; i++) {
        for (int j = 0; j < tempArray2[0].length; j++) {
          rowSums[i] += tempArray2[i][j];
        }
      }

      //Modified Bubble Sort of rowSum array (highest to lowest values)
      int temp;
      int i = 0;
      for(int j = rowSums.length; j > 0; j--){
        boolean isSwap = false;
        for (i = 1; i < j; i++) {
          if(rowSums[i-1] < rowSums[i]) {
            temp = rowSums[i-1];
            rowSums[i-1] = rowSums[i];
            rowSums[i] = temp;
            isSwap = true;
            //swaps rows in corresponding tempArray2 
            int [] temp2 = tempArray2[i-1];
            tempArray2[i-1] = tempArray2[i];
            tempArray2[i] = temp2;
          }
        }

        if(!isSwap){
          break;
        }
      }  

      //Prints sorted array 
      System.out.println("Sorted array: ");
      for (i = 0; i < tempArray2.length; i++) {
        for (int j = 0; j < tempArray2[i].length; j++) {
          System.out.print("$"+ tempArray2[i][j] + " ");
        }
        System.out.println();
      }
    } 

您可以尝试这种方式。 我已经解决了。

 public class Solution{
          public static void sortedArrayByRowTot() {
            int [][] salaryArray = { {4,5,6},{3,4,5},{2,3,4} };
            int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];

            for (int i = 0; i < salaryArray.length; i++) {
              for (int j = 0; j < salaryArray[i].length; j++) {
                tempArray2[i][j] = salaryArray[i][j];
              }
            }

            // Buble Sort to store rowSums
            int [] rowSums = new int [tempArray2.length];
            for (int i = 0; i < tempArray2.length; i++) {
              for (int j = 0; j < tempArray2[0].length; j++) {
                rowSums[i] += tempArray2[i][j];
              }
            }

            //Buble Sort by Rows Sum (Lowest Value to Highest)
            int temp;
            int i = 0;
            for(int j = rowSums.length; j > 0; j--){
              boolean isSwap = false;
              for (i = 1; i < j; i++) {
                if(rowSums[i-1] > rowSums[i]) {
                  temp = rowSums[i-1];
                  rowSums[i-1] = rowSums[i];
                  rowSums[i] = temp;
                  isSwap = true;
                  //swaps rows in corresponding tempArray2 
                  int [] temp2 = tempArray2[i-1];
                  tempArray2[i-1] = tempArray2[i];
                  tempArray2[i] = temp2;
                }
              }

              if(!isSwap){
                break;
              }
            }  
            /** No Need.
            for (int k = 0; k < tempArray2.length; k++) {
            temp = tempArray2[i-1][k];
            tempArray2[i-1][k] = tempArray2[i][k];
            tempArray2[i][k] = temp;
          }
          */

            for (int b = 0; b < tempArray2.length; b++) {
            for (int c = 0; c < tempArray2[b].length; c++) {
              System.out.print(tempArray2[b][c] + " ");
            }
          }
          }

        public static void main(String[] args) {
            sortedArrayByRowTot();
        }
      }

暂无
暂无

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

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