简体   繁体   English

如何将sum的值存储在新数组b中

[英]How do i store the value of sum in the new array b

public class HW9P1
{
   public static void main(String[] args)
   {
      /*int[][] a1 = { { 4, -2, 10 }, 
                     { 19, 3, -5 } };
      int[] b1 = coloumnsum(a1);*/

      int[][] a2 = { { 7, -10 }, 
                     { 4, 13 }, 
                     { 1, 0 } };
      int[] b2 = coloumnsum(a2);

      int i,j;
      for (i=0; i<b2.length; i++)
      {

         System.out.print(b2[i] + " ");


      }

  }
   public static int[] coloumnsum (int[][] a)
   {
      int[] b = new int[a[0].length];
      int i,j, sum= 7;

         for (j=0; j<a[0].length; j++)
         {
            for (i=0; i<a.length; i++)
            {
               sum = sum + a[i][j];


            }
         }
         return b;

   }
}

I have this code so far, which gives me the sum from the columns.Takes and 2d array and return 1d array. 到目前为止,我已经有了这段代码,这给了我来自column.Takes和2d数组并返回1d数组的总和。 when i run this code, i get two 00 For ex {2,4} {1,9} sum should be {3,13} 当我运行此代码时,我得到两个00,对于前{2,4} {1,9},总和应为{3,13}

You aren't storing your sum back into the array b . 您没有将sum存储回数组b You actually don't need sum , just add the values directly in the array. 实际上,您不需要sum ,只需将值直接添加到数组中即可。 Like, 喜欢,

public static int[] coloumnsum(int[][] a) {
    int[] b = new int[a[0].length];
    for (int j = 0; j < a[0].length; j++) {
        for (int i = 0; i < a.length; i++) {
            b[j] += a[i][j];
        }
    }
    return b;
}

When I make that change your current code produces (as expected) 当我进行更改时,您当前的代码会产生(按预期)

12 3
  1. I don't know why you're starting the sum at 7 if all you want is to sum the columns of the 2D array. 我不知道如果要对2D数组的列求和,为什么要从7开始求和。
  2. you're not assigning anything to accumulator array b . 您没有为累加器数组b分配任何东西。

below illustrates the correct approach to sum the columns: 下面说明了汇总列的正确方法:

public static int[] coloumnsum (int[][] a)
{
       int[] b = new int[a[0].length];
       for (int i = 0; i < a[0].length; i++){
           for (int j = 0; j < a.length; j++)
               b[i] += a[j][i];                   
       }        
       return b;  
}

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

相关问题 如何将两个数组字符串的值求和并存储到Java Selenium中的另一个数组列表中? - How to do sum of two array string's value and store into another array list in java selenium? 如何复制二维数组并添加新行以在 Java 中存储列的总和? - How can I copy a 2D array and add a new row to store the sum of the columns in Java? 为什么我不能做 a 和 b 的总和? - Why can I not do sum of a and b? 如何对数组列表的元素求和? - How do I sum the elements of an array list? 如何将新的客户端信息存储在数组中并能够在 java 中再次查看? - How do I store new client information in array and be able to view it again in java? 在Java中,如何使用循环实例化新对象,将它们存储在数组中,然后使用它们? - In Java, how do I use a loop to instantiate new objects, store them in an array, and then use them? 如何找到与特定值最接近的数组元素的总和? - How do I find the closest possible sum of an Array's elements to a particular value? 如何将这些打印的字母存储到数组中? - How do I store these printed letters into an array? 如何将数组存储到ArrayList的元素中? - How do I store an array into an element of ArrayList? 如何将正值和负值(可以分别存储)存储到数组中,然后打印出来? - How do I store positive and negative value (may be separately?) into an array, and print them out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM