0

I am trying to add 2 arrays together and return the sum of it, but it doesn't produce any output. 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;
    }
}
0

4 Answers 4

3

First, you need to instantiate the array correctly:

int[] sumArray = new int[ar1.length];

Then, if you want to see what is the result, you need to do something with it (print it maybe...):

System.out.println( Arrays.toString( sumA(ar1, ar2)) );
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to add arrays with different lengths:

private static int[] sumA(int[] ar1, int[] ar2) {
    int[] sumArray = new int[Math.max(ar1.length, ar2.length)];

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

If not, BobTheBuilder's answer is sufficient.

2 Comments

Wouldn't it be safer to use Math.min() instead of Math.max()? Using Math.max() may result in accessing non-existent elements in a shorter array (in case the lengths of arrays differ).
There's a check for this - "(i < ar1.length ? ar1[i] : 0)".
1

Your error is here:

int[] sumArray = new int[0]; <---

you need to instantiate the array with the length of one of the two parameters.

Try this:

int[] sumArray = new int[ar1.length];

or this

int[] sumArray = new int[ar2.length];

and if you want to show results:

int[] sumArray = new int[ar1.length];

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

Comments

0

working demoThat's because you are not writing any system.out.println(); statements in your code. Try this code:

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};

    int sum[]=sumA(ar1, ar2);

    for(int i: sum)
    {
        System.out.println(i);
    }
}

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

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

See the image attached.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.