按行总和对2D数组排序

按行总和对2D数组排序

本文介绍了Java-按行总和对2D数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to write a method that swaps the rows of a 2D array in order of increasing row sum.

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

For example, if I have the following 2d array:

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

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

I would want it to output an array as so:

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

方法:

a.)取每一行的总和,然后将这些总和制成一维数组

a.) take the sums of each row and make a 1D array of the sums

b.)对rowSum数组进行气泡排序

b.) do a bubble sort on rowSum array

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

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

d.) then print the newly row sorted array.

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

Here's my code so far:

      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部分?

Not sure if I am doing part c of my methodology correctly?

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

It keeps saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2"

推荐答案

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

I was able to solve my question. Thanks.

    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();
      }
    }

这篇关于Java-按行总和对2D数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:55