CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    32

    Adding int arrays?

    I have a collection of numbers in two arrays that I need to combine into one single object, whether that final object is an array or string makes no difference. I've tried various things including converting the arrays to strings and then converting the strings to int's but I run into some kind of error (usually the NumberFormatExceptoin). What is the best way about doing this? I tried finding a way to convert a sequence of numbers in an array into a single int but haven't had any luck, i would definitely make my life a lot easier if this was possible.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Adding int arrays?

    If you need the int values to be int values and you can combine the 2 arrays into one array then why would you even want to convert them to and from strings?

    The simplest way of combining the two arrays into one array is to define a new array of the correct length and use System.arraycopy() to copy the 2 arrays into the new array.
    I tried finding a way to convert a sequence of numbers in an array into a single int but haven't had any luck, i would definitely make my life a lot easier if this was possible.
    This would only be possible if you had a few very small numbers. Java ints are 32 bits long so for instance you could have 8 4 bit numbers. If you used a long you would have 64 bits to play with but this is still very limited.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2009
    Posts
    32

    Re: Adding int arrays?

    By adding two arrays together, I mean that if array1[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9}
    and array2[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,}, AddedArrays[] should be equal to {3,4,5,6,7,9,0,1,1,3,4,5,6,7,9,0,1,1} which when printed to the screen will appear as one single correctly added number (including carries): 345679011345679011.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Adding int arrays?

    By adding two arrays together, I mean that if array1[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9}
    and array2[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,}, AddedArrays[] should be equal to {3,4,5,6,7,9,0,1,1,3,4,5,6,7,9,0,1,1}
    So what has happened to the 3 extra 2's in the second array or is this a typo?

    Adding 2 arrays like this is easy (although I'm at a loss as to why you are doing this). Create an array that is 1 longer than the longest array (to allow for any carry on the most significant digit) and loop over the arrays in reverse order adding each pair of numbers. If the resultant number is greater than or equal to 10, subtract 10 and add the carry to the next pair.

    When printing out the array, loop over it ignoring leading zeros and once you have reached a non zero number print every digit.
    Last edited by keang; September 25th, 2010 at 07:29 AM. Reason: Oops: If the resultant number is greater than 10 should of course be "If the resultant number is greater than or equal to 10"
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 2009
    Posts
    32

    Re: Adding int arrays?

    Thank you so much! That actually makes quite a bit of sense, now I'll try to implement it. It was actually part of a project that was due two days ago, but its nice to know how this would work if I ever need to do something similar again. Would you be kind enough to detail in words how the same thing would be done with multiplication instead of addition?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Adding int arrays?

    Would you be kind enough to detail in words how the same thing would be done with multiplication instead of addition?
    Multiplication would have to be handled slightly differently as you don't know how many digits the resulting number is likely to have. You would be better off putting the result into an ArrayList rather than an array as an ArrayList will grow to whatever size is required.

    Other than that the process is basically the same although you have to apply the rules of long multiplication instead of addition.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Sep 2010
    Posts
    1

    Re: Adding int arrays?

    you can use the Arrays class in the java.util package
    Java Programming

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Adding int arrays?

    Quote Originally Posted by xcool101Man View Post
    you can use the Arrays class in the java.util package
    Perhaps you could explain how the Arrays class might help here?

    So how does that link help the OP with his problem?

    Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter...
    Eric Raymond
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured