在Java中按行总和交换2d数组中的行

时间:2015-11-09 05:41:03

标签: java arrays

我实现了这个,但我不确定这是正确的

   Example : int [][]={{2,1,0},{2,8,9},{1,1,0}}

在上面的行1 (2+1+0=3)中的元素总和小于行2(2+8+9=19)中的元素总和小于行3总和(2)的元素总和。

最终数组应该像{{2,8,9},{2,1,0},{1,1,0}}

    int arr[5][5];
    int rowSum[5];
    int sum = 0;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
        sum = sum + arr[i][j];
        }
        rowSum[i] = sum;
        sum=0;
    }

    int swap;
    //now sorting
    for (int c = 0 ; c < ( n - 1 ); c++)
      {
        for (int d = 0 ; d < n - c - 1; d++)
        {
          if (rowSum[d] > rowSum[d+1]) /* For decreasing order use < */
          {
            swap       = rowSum[d];
            rowSum[d]   = rowSum[d+1];
            rowSum[d+1] = swap;

        //swapping original array
        for(int i=0;i<5;i++){
           swap = arr[d][i];
           arr[d][i] = arr[d+1][i];
               arr[d+1][i] = swap;
        }
          }
        }
     }

3 个答案:

答案 0 :(得分:2)

使用Java 8功能:

/*
 * custom.js
 * @author : Rohil Mistry
 */

(function($){
    $(document).ready(function(){
        /*updated_checkout event*/
        $(document.body).on('updated_checkout', function(){
            /*Make an AJAX call on updated_checkout event*/
            $.ajax({
                type:       'POST',
                url:        custom_values.ajaxurl,
                data:       {action:'check_for_coupon'},
                success:    function( result ) {
                    console.info(result);
                    if(result.success){
                        $(".coupon_value").html(result.discount);
                    }
                    else{
                        $(".coupon_value").html('');
                    }
                }
            });
        });
    });
})(jQuery);

答案 1 :(得分:1)

Integer[][] numbers = new Integer[][]{{2,1,0},{2,8,9},{1,1,0}};
System.out.println("Before:");
for(Integer[] row : numbers) {
  for(Integer num : row) {
    System.out.print(num+",");
  }
  System.out.println("");
}

Arrays.sort(numbers, new Comparator<Integer[]>(){
  @Override
  public int compare(Integer[] o1, Integer[] o2) {
    Integer sumArray_1 = 0;
    Integer sumArray_2 = 0;
    for(int i = 0; i < o1.length; ++i) {
      sumArray_1 += o1[i];
    }

    for(int i = 0; i < o2.length; ++i) {
      sumArray_2 += o2[i];
    }

    return sumArray_2.compareTo(sumArray_1); //Decending order 
  }
});
System.out.println("After:");
for(Integer[] row : numbers) {
  for(Integer num : row) {
    System.out.print(num+",");
  }
  System.out.println("");
}

输出:

Before:
2,1,0,
2,8,9,
1,1,0,
After:
2,8,9,
2,1,0,
1,1,0,

答案 2 :(得分:0)

使用以下代码段,

        int[][] temp = {{2,1,0},{2,8,9},{1,1,0}};
        Arrays.sort(temp, new Comparator<int[]>() {
        @Override
        public int compare(int[] a, int[] b) {
            return Integer.compare(b[1], a[1]);
        }
        });
        System.out.println("After applying comparator");
        for(int[] arr:temp){
            for(int val:arr){
                System.out.print(val+" ");
            }
            System.out.println("");
        }

它将显示如下输出

After applying comparator
2 8 9 
2 1 0 
1 1 0