Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Adding up the elements of two arrays and putting the resulting sum in another array

Johnathan Beck
Greenhorn
Posts: 7
I am working on how to add the contents of two arrays and putting the results on another array. The problem is that I am not too sure on how to do it properly.

The first array I called ary1. It is a double type with unknown length. This length is determined by my JUnit test class.
The second array I labeled as ary2. ary2 is a copy of ary1. Therfore I casted it as the following in my adding arrays method.

Where y is a parameter passed from the NVectorADT interface. There isn't a return statement yet because I haven't determined what needs to be returned. I am thinking the result array.

Lastly the result array is a double where it's size is determined by the size of ary1. I created size field and in the constructor assigned size as ary1.length.

Here is the code of what I have done thus far.

Here are my thoughts of how to do this.

I know I am going to need a while loop that will terminate at the end of the length of ary1. I know in this loop I will need three for loops, in the first two for loops I need to mull through ary1 and ary2 and store each number in local variables. The last for loop will store the result of my caculation in the result array.

I am thinking the looping structure might look something like this.

While
for loop - ary1
for loop - ary2
for loop - store sum of ary1[i] and ary2[i] in aryResult[i]
end

However I don't know how to get a element in ary1, adding it to it's corresponding element in ary2 and putting the result in it's corresponding aryResult (ex. ary1[1] + ary2[1] = aryResult[1]). Also the thought has crossed my mind if I even will need three for loops. Couldn't I just use one for loop to mull through ary1 and ary2, do the operation, and store the result in aryResult?

Any thoughts would be appreciated.

Unnar Björnsson
Ranch Hand
Posts: 164
If I understand you correctly you are overthinking the problem, you only need one loop, the loop index maps each element of ary1 to ary2 and stores it in result array in the same iteration.
and store each number in local variables.

No you don't need any extra variables for the task.

However if ary1 and ary2 are of different lengths, special care needs to be taken to either catch IndexOutOfBoundsException or perform checks each iteration.

Johnathan Beck
Greenhorn
Posts: 7
Unnar Björnsson wrote:If I understand you correctly you are overthinking the problem, you only need one loop, the loop index maps each element of ary1 to ary2 and stores it in result array in the same iteration.

Yeah it's something I tend to do, overthinking a problem. I know it's a bad habit when it comes to programming and believe me I am working on it. I do appreciate your honesty.

Unnar Björnsson wrote:However if ary1 and ary2 are of different lengths, special care needs to be taken to either catch IndexOutOfBoundsException or perform checks each iteration.

You know I didn't even think of that. I would figure my constructor would check that however it only checks to see if ary1 is null or empty.

So I took your idea and going with something like this.

I think it will work. So I'm gonna try it out.

edit: It didn't work. my IDE is saying array is required but DVector was found.

I'm thinking I might have to make an ary2 a true array. But honestly I don't know. It makes sense though if that's it.

Unnar Björnsson
Ranch Hand
Posts: 164

I'm thinking I might have to make an ary2 a true array. But honestly I don't know. It makes sense though if that's it.

ary2 is not an array but an object therefore you can't use bracket operations to access its values, you'll need to define getters and setters as well as size() methods to manipulate its values. You might consider extending ArrayList for that purpose.

aryResult[size] = ary1[i] + ary2[size];

Yes java is throwing IndexOutOfBoundsException because an array of size N contains elements in places from 0 to N-1, you are asking for an element in the (last+1)th place
You want to add together elements ary1[0]+ary2[0] ary1[1]+ary2[1] ... right?

Johnathan Beck
Greenhorn
Posts: 7
Unnar Björnsson wrote:You want to add together elements ary1[0]+ary2[0] ary1[1]+ary2[1] ... right?

Essentially yes. It should go something like this ary1[i] + ary2[i] and store the caculation in aryResult[i] and keep going through the array.

The DVector needs to have one constructor, that takes an array as a parameter. I then need to make a copy of that array. The constructor should throw an exception if there's anything wrong with the array parameter. Which in this case the only thing I can think of is either a null or empty array. I think I got that part covered in the constructor.

It then goes on when it passes the check then I need to make a copy. My instructor drew this diagram. The ( ) represents arrays.

So I am wondering does the casting DVector ary2 = (DVector) y make a copy of the array object?

Johnathan Beck
Greenhorn
Posts: 7
Unnar I am going to post the ADT interface and the DVector classes. Maybe it will help you out in helping me figuring this out.

NVectorADT as given by my instructor.

DVector Class

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
37
Johnathan Beck wrote:So I am wondering does the casting DVector ary2 = (DVector) y make a copy of the array object?

No, casting does not copy objects or do any "magical" conversions. A cast is nothing more than you telling the compiler "I have an object here, and I want you to treat it as if it is a DVector".

Unnar Björnsson
Ranch Hand
Posts: 164
You are on the right track, you just need to add getElement() and getSize() functions in DVector class and use that to access elements of ary2

Another thing aryResult is not an instance variable, it should be a local variable in the implemented functions