java中两个数组的总和

时间:2015-03-11 12:29:50

标签: java arrays

我是Java编程的新手。我在这里有一个任务告诉我找到两个数组的总和。真的很简单,但我很难搞清楚所有的语法。以下是我正在讨论的问题Here is the question that I am talking about,以下是我已经走了多远。

主类

package arraysum;
import java.util.Scanner;

public class ArraySum extends ArrayTwo{


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int result[] = new int[10];


    }
}

Sub Class One

 package arraysum;

import java.util.Scanner;

public class ArrayOne{
    int arr1[] = new int[10];
      Scanner input = new Scanner(System.in);

    public ArrayOne(){
      System.out.println("Enter the elements of the 1st array");
        for(int x=0;x<10;x++)
        {
            System.out.println("Enter an element");
            arr1[x]=input.nextInt();
        }
    }
}

第二类

    package arraysum;

public class ArrayTwo extends ArrayOne{
    int arr2[] = new int[10];

    public ArrayTwo(){
    System.out.println("Enter the elements of the 2nd array");
        for(int y=0;y<10;y++){
            System.out.println("Enter an element");
            arr2[y]=input.nextInt();
        }
    }
}

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:1)

我没有足够的声誉发表评论,所以我会回答。

您不需要为每个数组创建一个类。只需按照以下步骤(只是一个伪代码)

initialize array1;
foreach elem in array:
    sumArray[pos] = read user input;

initialize array2;
foreach elem in array:
    sumArray[pos] = read user input;

initialize sumArray
foreach elem in array:
    sumArray[pos] = array1[pos] + array2[pos];

答案 1 :(得分:1)

您的sum和subtract方法应如下所示

public class ArrayMath {
    public static int[] sum(int [] arrOne,int [] arrTwo) {
        if(arrOne.length!=arrTwo.length) {// check if length is same
            return null;
        }
        int[] returnArray = new int[arrOne.length];
        for(int i=0;i<arrOne.length;i++) { // adding with same index
            returnArray[i]=arrOne[i]+arrTwo[i];
        }
        return returnArray;
    }

    public static int[] subtract(int [] arrOne,int [] arrTwo) {
        if(arrOne.length!=arrTwo.length) { // checking length is same
            return null;
        }
        int[] returnArray = new int[arrOne.length];
        for(int i=0;i<arrOne.length;i++) { // subtracting with same index
            returnArray[i]=arrOne[i]-arrTwo[i];
        }
        return returnArray;
    }
}

你的主要应该是这样的:

public class ArraySum {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the size of the arrays : ");
      int size = input.nextInt();
      int arrayOne[] = new int[size];
      int arrayTwo[] = new int[size];
      for(int i=0;i<size;i++){
         System.out.println("Enter the "+i+"th elements of the 1st array");
         arrayOne[i]=input.nextInt();
         System.out.println("Enter the "+i+"th elements of the 2st array");
         arrayTwo[i]=input.nextInt();
      }

      // Now you can call the methods
      int[] additionArray=ArrayMath.sum(arrayOne,arrayTwo);
      int[] subtractionArray=ArrayMath.subtract(arrayOne, arrayTwo);

      if(additionArray==null){
         System.out.println("Both array are not of same size hence cannot add");
      } else {
         System.out.println("Addition of array is");
         Arrays.sort(additionArray);
         for(int i=0; i<additionArray.length;i++){
            System.out.println(additionArray[i]+" ");
         }
      }
      if(subtractionArray==null){
         System.out.println("Both array are not of same size hence cannot subtract");
      } else {
         System.out.println("Subtraction of array is");
         Arrays.sort(subtractionArray);
         for(int i=0; i<subtractionArray.length;i++){
            System.out.println(subtractionArray[i]+" ");
         }
      }

   }
}

另一种方法是。 ArrayMath类

public class ArrayMath {
    private int[] array;

    public ArrayMath(int[] arr){
        this.array=arr;
    }

    public int[] sum(int [] arr) {
        if(array.length!=arr.length){
            return null;
        }
        int[] returnArray = new int[array.length];
        for(int i=0;i<array.length;i++) {
            returnArray[i]=array[i]+arr[i];
        }
        return returnArray;
    }

    public int[] subtract(int [] arr) {
        if(array.length!=arr.length){
            return null;
        }
        int[] returnArray = new int[array.length];
        for(int i=0;i<array.length;i++) {
            returnArray[i]=array[i]-arr[i];
        }
        return returnArray;
    }
}

主要课程:

public class ArraySum {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the size of the arrays : ");
      int size = input.nextInt();
      int arrayOne[] = new int[size];
      int arrayTwo[] = new int[size];
      for(int i=0;i<size;i++){
         System.out.println("Enter the "+i+"th elements of the 1st array");
         arrayOne[i]=input.nextInt();
         System.out.println("Enter the "+i+"th elements of the 2st array");
         arrayTwo[i]=input.nextInt();
      }

      ArrayMath arrayMath = new ArrayMath(arrayOne);
      // Now you can call the methods
      int[] additionArray=arrayMath.sum(arrayTwo);
      int[] subtractionArray=arrayMath.subtract(arrayTwo);

      if(additionArray==null){
         System.out.println("Both array are not of same size hence cannot add");
      } else {
         System.out.println("Addition of array is");
         Arrays.sort(additionArray);
         for(int i=0; i<additionArray.length;i++){
            System.out.println(additionArray[i]+" ");
         }
      }
      if(subtractionArray==null){
         System.out.println("Both array are not of same size hence cannot subtract");
      } else {
         System.out.println("Subtraction of array is");
         Arrays.sort(subtractionArray);
         for(int i=0; i<subtractionArray.length;i++){
            System.out.println(subtractionArray[i]+" ");
         }
      }

   }
}

答案 2 :(得分:0)

试试这个将给你添加

package com.arraysum;
import java.util.Scanner;
public class Addition {

    public static void main(String[] args) {

        System.out.println("Enter the two array size..");
        Scanner input = new Scanner(System.in);
        int size = input.nextInt();
        int[] one= new int[size],two = new int[size];
        System.out.println("Enter the array elements of Array-One");
        for (int i = 0; i <size; i++) {
            one[i]=input.nextInt();
        }
        System.out.println("Enter the array elements of Array-Two");
        for (int i = 0; i <size; i++) {
            two[i]=input.nextInt();
        }
        Addition(one,two);

    }

    private static void Addition(int[] one, int[] two){
        System.out.print("Array One"+"\t");
        for (int i = 0; i <one.length; i++) {
        System.out.print(one[i] + "\t");
        }
        System.out.println();
        System.out.print("Array Two"+"\t");
        for (int i = 0; i <two.length; i++) {
        System.out.print(two[i]+ "\t");
        }
        System.out.println();
        System.out.println("---------------------");
        int result[]=new int[one.length];
        for (int i = 0; i < result.length; i++) {
            result[i]=one[i]+two[i];
        }
        System.out.println("Sum Of Two Arrays ");
        System.out.print(" Sum "+"\t"+"\t");
        Arrays.sort(result);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i]+"\t");
        }
    }
}

答案 3 :(得分:0)

如果我理解你需要什么,你应该跳过处理类层次结构。 只需初始化两个数组,然后创建一个新的sum数组并复制其中的值。

import java.util.Arrays;

public class ArTestSum {
    public static void main(String... args) {
        int[] ar1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] ar2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] arSum = new int[ar1.length + ar2.length]; // it is OK because
                                                        // known in compile time

        int i = 0;
        for (int val : ar1) {
            arSum[i++] = val;
        }
        for (int val : ar2) {
            arSum[i++] = val;
        }
        System.out.println(Arrays.toString(arSum));
    }
}

答案 4 :(得分:0)

以下是您的问题的代码段。希望这对你有用。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int arr1[] = new int[10];
    int result[] = new int[10];

    System.out.println("Enter the elements of the 1st array");
    for (int x = 0; x < arr1.length; x++) {
        arr1[x] = input.nextInt();
    }

    int arr2[] = new int[10];
    System.out.println("Enter the elements of the 2nd array");
    for (int x = 0; x < arr2.length; x++) {
        arr2[x] = input.nextInt();
    }

    for (int i = 0; i < arr1.length; i++) {
        result[i] = arr1[i] + arr2[i];
    }
    Arrays.sort(result);
    for (int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
    }
}