Originally Posted by
pbrockway2
A few things:
If you are going to do multiple things with the array it is (as you have found) really, really important that you don't alter the array contents. The array - to use Barbara Liskov's term - is a "shared object". It is shared between the caller and the methods. That's what makes the "return" unnecessary in the other thread. And, as I think you have also seen, it means that the methods "can look, but must not touch" when it comes to the array.
I agree with you that making a copy of the array in the digit counting method will work, but it feels wrong. What about this: the differentDigits() method could create a small "counter" array, just 10 int elements wrong. Every time it sees a digit in the big array it increases the appropriate element in the counter array. Then to report how many different digits there are it need only count how many nonzero digits there are in the counter array. Since what you have works you might want to put this aside until later.
When you say that your code gives the "wrong" value, you seem to be basing this on what result you get after all sorts of other methods have been at work. Clearly neither me nor 2x4 nor anybody else here knows what those other methods are. A better approach (which I tried to hint at in my first reply) is to make the methods self-contained so each can be tested and verified on it's own. And document what the method in question is supposed to do - for your benefit as well as ours.
This approach will not make bugs go away. But it might shine light on them. In particular it might distinguish between whether a problem arises because the a method is faulty and whether the problem is that some other method has unwanted side effects.
You say "it works now". Does that mean the digits method? the factorial? both?