Win a copy of Design for the Mind this week in the Design forum!

# Adding elements of two arrays

Frank Ramirez
Greenhorn
Posts: 17
Hello everyone
If I have 2 arrays, let's say int [] arr1 = {1,2,3,4) and arr2 = {4,2,1,8}
I'd like to add every element of the arrays
arr1[3]+arr2[3]
arr1[2]+arr2[2]
arr1[1]+arr2[1]
arr1[0]+arr2[0]

If arr1[3]+arr2[3] >9, then I'd have to add 1 to arr1[2]+arr2[2]

The result would be like adding 1234 + 4218

If i'm adding arr1[n]+arr2[n] and the result is >9, I don't know how to add 1 to arr1[n-1]+arr2[n-1]

I'd appreciate any help with this.
Thank you

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
you do it exactly like you said...

write a loop that iterates across all the arrays

You have to be a little careful when you get to arr1[0], because if if arr1[0] + arr2[0] is greater than 9, you don't have another element to put the overflow into. (just something to think about...)

Frank Ramirez
Greenhorn
Posts: 17
Thank you Fred
That's exactly what I was trying to do and, of course, the same concern came to my mind.
Would it work with ArrayLists? I'm even considering if it would work with ArrayLists with different number of elements

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
it should..however, while ArrayLists will grow, they grow on the end. You may even consider storing your number backwards...so the 1's digit is in element 0, the 10's digit is in element 1, etc. that way growing the array list is more natural.

Frank Ramirez
Greenhorn
Posts: 17
Very true. I didn't consider the idea of inverting the order of the elements. But then, what would happen if the two Arraylists have different number of elements?

Rob Spoor
Sheriff
Posts: 20511
54
fred rosenberger wrote:

That should of course be arr1.size - 1.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
thanks Rob.

If the numbers are a different size... you have several options. you can either pad the smaller one with zeros, or just write your code so that if an element isn't there when you need it, it is created on the fly.

You're going to have to account for that situation whether you reverse the digits or not...

Frank Ramirez
Greenhorn
Posts: 17
Thanks Fred for all your responses
I was thinking about creating a method that compares both and as you said, pads the smallest one with zeros. I'd like to know how to do the second one that creates the elements on the fly.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
an ArrayList lets you do exactly that. read the API on it and see what method might work for you.

if you are forced to use an Array, you can still do it...find how many elements you need for the largest number, then simply create your arrays with one more element. When doing simple addition, you'll never need more than one extra 'slot' to store your overflow.

Frank Ramirez
Greenhorn
Posts: 17
Ok, this is the complete code I have so far. I can't figure out what I'm doing wrong. It doesn't give me what I expect, it gives some random characters. HELP PLEASE!

Henry Wong
author
Marshal
Posts: 20997
76

You can't just concat an int array. Well, you can, but it won't do what you want. In the concat, the toString() of the int array will be called, and you will get the symbol for int array, followed by the hashcode for the array. And this hashcode has nothing to do with the actual int elements.

Henry

pete stein
Bartender
Posts: 1561
Arrays are 0 based, and you're trying to use them as if they were 1 based.

Also, you're allowing the array to display itself using its innate toString method that does nothing but show its hashcode. You can display an array via java.util.Arrays.toString(...) or you could write your own method, say intArrayToString(int[] intArray).

pete stein
Bartender
Posts: 1561

Frank Ramirez
Greenhorn
Posts: 17
Thank you Pete and Henry for your responses. I'm trying to understand what you are telling me but I still don't get it.
- What do you mean when you say "concat an int array"? How can I print the elements of the array?
- Arrays are 0 based, and you're trying to use them as if they were 1 based. What do you mean with this?

Sorry if I keep asking, but I'm trying to learn and I can't figure this out.
Also, I was checking java.util.Arrays.toString(...) but I don't know how to use it. Any example please?
Thank you again for your help

Henry Wong
author
Marshal
Posts: 20997
76

Pete,

Can you post the link to the crossposted topic?

Henry

Frank Ramirez
Greenhorn
Posts: 17
Sorry, I was trying to post to the Sun Forums, but for some reason it didn't show the posts, if you notice, I tried the same post three times. Now I can see them but I wasn't able to see them by the time I was posting.
I apologize for the inconvenience. I wasn't in my interest to bother anyone, I'm only trying to learn.

pete stein
Bartender
Posts: 1561
Henry Wong wrote:

Pete,

Can you post the link to the crossposted topic?

Henry

I think that the person to ask this question of really is the original poster. It is his responsibility to post all links to all cross-posts in every cross-post. So I will leave it up to him to post the links to his cross-posts in the sun fora, elsewhere, and also for him to post a link to this thread in his sun fora posts etc...

Henry Wong
author
Marshal
Posts: 20997
76
Frank Ramirez wrote:
- What do you mean when you say "concat an int array"? How can I print the elements of the array?

Basically, this... "The sum of the two numbers is: " + finalArray ... doesn't do what you think it does.

One way to print the elements of the array is to use a loop.

Henry

Henry Wong
author
Marshal
Posts: 20997
76
Frank Ramirez wrote:
- Arrays are 0 based, and you're trying to use them as if they were 1 based. What do you mean with this?

It means that in Java, the first element is the zeroth element. But in your code, you start with one (or end with one), ignoring the first element in many cases.

Henry

Frank Ramirez
Greenhorn
Posts: 17
Again, I thank you for your help.
Mr. Stein, I apologize again for the inconvenience.
Honestly it wasn't my intention to offend you or any other memeber of this forum or the other forum where I posted.
Here is the link to the other post:

Frank Ramirez
Greenhorn
Posts: 17
Henry Wong wrote:
Frank Ramirez wrote:
- What do you mean when you say "concat an int array"? How can I print the elements of the array?

Basically, this... "The sum of the two numbers is: " + finalArray ... doesn't do what you think it does.

One way to print the elements of the array is to use a loop.

Henry

Yes, that is exactly what I did and it worked. Thank you for your help

Frank Ramirez
Greenhorn
Posts: 17
Henry, thanks for your patience. I didn't understand what you meant with base 0 and 1. I now arrays start with position zero. I had assigned the value 0 to the first element of my array. That way, if the sum of the element in position 1 was greater than 9, then my number in posiion 0 would be 1.

I found a way to do what I wanted

Thanks again for all your help.

Henry Wong
author
Marshal
Posts: 20997
76

try .... 1119 + 1119. I don't think your code will work in this case.

Henry

Frank Ramirez
Greenhorn
Posts: 17
Henry Wong wrote:
try .... 1119 + 1119. I don't think your code will work in this case.

Henry

Arggggg.... You are right! How can I do this?

Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Frank Ramirez wrote:
Arggggg.... You are right! How can I do this?

The real question is:

how did he know?

Frank Ramirez
Greenhorn
Posts: 17
Seems to work now (crossing fingers....)

Campbell Ritchie
Sheriff
Posts: 48652
56
You can probably replace the bit about -= 10 by the % and / operators. Even though division is probably a slow arithmetic operation.