Renea S.
Renea S.

Reputation: 83

Java How to find the sum of sum of the elements in two different arrays?

I need to fill a new array, array3 with the sum of the corresponding elements in array1 and array2. The arrays are designated a1, a2, and a3.

double[] a1 = {1.2, 2.3, 3.4, 4.5, 5.6};
double[] a2 = {1.0, 2.0, 3.0, 4.0, 5.0};

a3 should also be an array of 5 doubles. It should come out to be {2.2, 4.3, 6.4, 8.5, 10.6}.

I have tried to figure it out, but it keeps messing up. Thank you for any help!

Here is what I have so far:

double[] a1 = {1.2, 2.3, 3.4, 4.5, 5.6}; 
double[] a2 = {1.0, 2.0, 3.0, 4.0, 5.0}; 
int i = 0; 
double [] a3 = new double[5]; 
for( i =0; i < a1.length; i++) {
   a3[i] = a1[i] + a2[i];  
}
System.out.println(a3[i]);

Upvotes: 0

Views: 1262

Answers (6)

smaant
smaant

Reputation: 671

Guys already helped you with IndexArrayOutOfBounds caused by System.out.println(). Apart from this problem I would recommend change

double [] a3 = new double[5];

to

double [] a3 = new double[a1.length];

Otherwise, if you'll increase size of a1, you'll get another IndexArrayOutOfBounds error.

Upvotes: 0

Cedric Simon
Cedric Simon

Reputation: 4659

Your code is fine. Just remember variable i was incremented so to print last value, it must be i-1.

public class Test {
    public static void main(String[] args) {
        double[] a1 = {1.2, 2.3, 3.4, 4.5, 5.6};

        double[] a2 = {1.0, 2.0, 3.0, 4.0, 5.0};

        int i = 0;

        double [] a3 = new double[5];


        for( i =0; i < a1.length; i++){
            a3[i] = a1[i] + a2[i];  
        }

        System.out.println(a3[i-1]);

        for( i =0; i < a1.length; i++){
            System.out.println("a["+i+"] = "+a3[i]);
        }
    }
}

Upvotes: 1

JavaNewbie_M107
JavaNewbie_M107

Reputation: 2037

The IndexOutOfBoundsException is caused as the value of i in your print statement is equal to a3.length(). The elements of an array are indexed from 0 to array.length-1, not upto array.length. The rest of your code is absolutely fine. So, try this:

for( i =0; i < a3.length; i++){
     System.out.println(a3[i]);
}

or this

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

When printing the values in an array, you can use either of these two approaches, because System.out.println() does NOT print all the elements of the array.

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

double[] a1 = {1.2, 2.3, 3.4, 4.5, 5.6};

double[] a2 = {1.0, 2.0, 3.0, 4.0, 5.0}; 

double [] a3 = new double[5];


for(int i=0; i < a1.length; i++){
    a3[i] = a1[i] + a2[i];
}
for(int i=0; i < a1.length; i++){
    System.out.println(a3[i]);  
}

This is a little bit better solution. Anyway, the core of your problem is what you're doing with i. When you're trying to iterate through the elements of arrays using for loops, keeping i scoped to just the for loop can help avoid some of the issues you ran into with your original question.

If you keep i scoped to just the for loops, then your compiler should throw a warning before you even try to compile because your original output statement would not even have known about a variable called i.

Upvotes: 2

Gurminder Singh
Gurminder Singh

Reputation: 1755

This

for( i =0; i < a1.length; i++){
  a3[i] = a1[i] + a2[i];  
}

System.out.println(a3[i]);

should be

for( i =0; i < a1.length; i++){
  a3[i] = a1[i] + a2[i];  
  System.out.println(a3[i]);
}

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

You just need to print all the elements of a3 array to see the desired result:

for( i =0; i < a3.length; i++){
     System.out.println(a3[i]);
}

Currently you are trying to display the result using one statement:

System.out.println(a3[i]);

which will throw null pointer exception because the value of i will be 5 at the end of loop.

Upvotes: 1

Related Questions