java - 查找数组中的差异

标签 java arrays

我不确定如何设置差异以存储在数组差异中。存储的数字应该是 5-(1+2+3), 7-(1,2,4), 8-(3,5,9) :输出应该是differences[0]= 1,differences[1] = 0,差异[2] = 9

  import java.util.Scanner;

    public class Main {
      public  static int[][] Array = { { 5, 1, 2, 3 }, { 7, 1, 2, 4 }, { 8,3,5,9 } };  //My 2D array//
      int [] differences = new int [3];

      public static int[] Sum(int[][] array) {
        int index = 0; //setting the index to 0//
        int temp[] = new int[array[index].length]; //making a temperary variable//
        for (int i = 0; i < array.length; i++) {
          int sum = 0;
          for (int j = 1; j < array[i].length; j++) {
            sum += array[i][j]; //going to add the rows after the first column//
          }
          temp[index] = sum;

          for(int a = 0; a<differences.length; a++){
            if(sum != array[index][0])
              sum -= array[i][j];




              System.out.println("the first integer " + array[index][0] + " the answer is " + sum); //going to print out the first integer each row and print out the sum of each row after the first column//
            index++; //index is going to increment//
          }
          return temp;
        }

        public static void main(String[] args) {
          new Main().Sum(Array);
        }
      }

输出:

    the first integer 5 the answer is 6
    the first integer 7 the answer is 7
    the first integer 8 the answer is 17

最佳答案

既然任务这么简单,你为什么要把它复杂化呢? :)

public int[] Sum(int[][] array)
    {
    int sum;
    for(int i = 0; i < Array.length; i++)
        {
        sum = Array[i][0] * -1;

        for(int j = 1; j < Array[i].length; j++)
            {
            sum += Array[i][j];
            }

        differences[i] = sum;
        }

    return differences;
    }

关于java - 查找数组中的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21582142/

相关文章:

java - 我的应用程序抛出 'java.io.IOException: could not create AudioData object'?

java - Spring Boot @Scheduled 不适用于数据库事务

java - 代币生成方案比较

javascript - 在javascript中将字符串连接成一个数组

c++ - 了解将数组传递给没有其大小的函数时丢失的信息

php - 糟糕的 PHP ArrayObject 设计

c - "Double free or corruption"函数内部错误?

java - 将动态json文件解析为对象的hashmap

c++ - 指向二维数组的指针

Java 正则表达式