本文介绍了将2个数组加在一起并返回它的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将2个数组加在一起并返回它的总和,但它不会运行。我想知道为什么?







I am trying to add 2 arrays together and return the sum of it, but it doesn't run. I wonder why?



public class MyProgram
{
    public static void main (String[] args)
    {
        new MyProgram().start();
    }

    public void start()
    {
        int[] ar1 = {3, 8, 4, 9, 5, 5, 23, 14};
        int[] ar2 = {33, 23, 41, 9, 17, 51, 23, 45};

        sumA(ar1, ar2);
    }

    private int[] sumA(int[] ar1, int[] ar2)
    {
        int[] sumArray = new int[0];

        for (int i = 0; i < ar1.length; i++)
        {
            sumArray[i] = ar1[i] + ar2[i];
        }
        return sumArray;
    }
}

推荐答案


int SumOfArray = 0;
for (i = array1.LowerBound; array1.UpperBound; i++)
{
    SumOfArray = SumOfArray + array1[i];
}

for (i = array2.LowerBound; array2.UpperBound; i++)
{
    SumOfArray = SumOfArray + array2[i];
}

Return SumOfArray;





但是更容易使用 []或 [],提供与Linq类似的功能。



But much, much easier is to use Coollection[^] or Lambda Expressions[^], which provide functionality similar to Linq.


int[] sumArray = new int[0];



没有在ar1找到属性,在这里看错字


There is no ength property in ar1, watch typo here

ar1.length


这篇关于将2个数组加在一起并返回它的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:27