aspose file tools*
The moose likes Beginning Java and the fly likes Adding elements of two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding elements of two arrays" Watch "Adding elements of two arrays" New topic
Author

Adding elements of two arrays

Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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

Joined: Oct 02, 2003
Posts: 11320
    
  16

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...)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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

Joined: Oct 02, 2003
Posts: 11320
    
  16

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

Joined: Feb 12, 2010
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

fred rosenberger wrote:

That should of course be arr1.size - 1.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

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

Joined: Feb 12, 2010
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

Joined: Oct 02, 2003
Posts: 11320
    
  16

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

Joined: Feb 12, 2010
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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
pete stein
Bartender

Joined: Feb 23, 2007
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

Joined: Feb 23, 2007
Posts: 1561
Please read this FAQ as it pertains to you and not reading it understanding it, and addressing it could make people not want to help you in the future: BeForthrightWhenCrossPostingToOtherSites
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

pete stein wrote:Please read this FAQ as it pertains to you and not reading it understanding it, and addressing it could make people not want to help you in the future: BeForthrightWhenCrossPostingToOtherSites


Pete,

Can you post the link to the crossposted topic?

Henry
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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

Joined: Feb 23, 2007
Posts: 1561
Henry Wong wrote:
pete stein wrote:Please read this FAQ as it pertains to you and not reading it understanding it, and addressing it could make people not want to help you in the future: BeForthrightWhenCrossPostingToOtherSites


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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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

Joined: Feb 12, 2010
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:
http://forums.sun.com/thread.jspa?threadID=5428198
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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

Joined: Feb 12, 2010
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
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40


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

Henry
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
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

Joined: Sep 14, 2009
Posts: 1665
    
  11

Frank Ramirez wrote:
Arggggg.... You are right! How can I do this?


The real question is:

how did he know?


When you do things right, people won't be sure you've done anything at all.
Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
Posts: 17
Seems to work now (crossing fingers....)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38888
    
  23
You can probably replace the bit about -= 10 by the % and / operators. Even though division is probably a slow arithmetic operation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding elements of two arrays