2

So I have an two arrayLists that represent two numbers. This is so I don't have to use BigInt. so for example

ArrayList<Integer> LargeInt = [2,3,6,4] would really equal 2,364
ArrayList<Integer> LargeInt2 = [8,7,9,4,6] would be 87,946

my goal is to figure out a way to multiply the two numbers and make the answer to a string. I know that multiplying two numbers would need to be put into another array before put into a string so it won't crash with larger numbers. I also know it will be one for loop put into another. But i am finding it difficult to make a code that multiplies the two numbers. the two arrays being multiplied can be any number.

Mat
  • 202,337
  • 40
  • 393
  • 406
user1670252
  • 47
  • 1
  • 1
  • 8

1 Answers1

5

Assuming that this is homework, here is a no-code explanation of what you need to do:

  • Define a class that wraps ArrayList<Integer>; let's say you call it ArrayInt
  • Define an operation that adds two ArrayInts together, and returns a third ArrayInt that equals their sum. You can do it digit by digit, taking care of a possible carry into an extra digit, so you need to size your result accordingly.
  • Define an operation that multiplies your number by a power of ten by adding zeros to the array list. Again, the operation should return a new ArrayInt, rather than modifying the current one
  • Define an operation that multiplies a number by a single digit. You can use multiplication, or a simple loop that uses addition. The loop would not run more than nine times, so it shouldn't be too bad.
  • Combine the three operations that you have (addition, multiplication by a digit, and multiplication by a power of ten) into a simple multiplication algorithm that you learned in the elementary school.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I would agree on this. But there might be approach to save some runtime, follow the rules of Bitwise Multiplication. You may want to take a look the code snippet presented [here](http://stackoverflow.com/questions/4456442/interview-multiplication-of-2-integers-using-bitwise-operators) to give you an idea how it is done. – David B Sep 14 '12 at 04:59
  • @Dr.Java I seriously doubt that saving time is a goal here: after all, it's a homework exercise. – Sergey Kalinichenko Sep 14 '12 at 09:36
  • maybe, but that's only a suggestion. – David B Sep 14 '12 at 09:58