Well you didn't need the if statement before the System.out at the end since this would be checked in every loop. You could have just put it after the loop completed.
Also, the array itself was unnecessary since you defined "anArray[i]=i" at the beginning of every loop. You could have removed the array completely and just done "add = add + i" or "add += i" for short.
Finally, you could have used mathematics to determine that the sum of integers from 1 to N is defined to be: (n^2 + n)/2 although the proof of this normally requires induction.
By the way, note that you get 4950 since you are really summing from 0 to 99, not 1..100. If you apply the formula to 99 it works out to be exactly 4950. Keep in mind this formula is a shortcut and may not be what your teacher wanted. Its not often such short cuts exist but when they do its great to be able to use them. [ November 08, 2005: Message edited by: Scott Selikoff ]
Joined: Oct 17, 2005
Thanks for the tips, i didnt fully understand what you mean by remove array, i presume you mean the "anArray[i]+anArray[i]"?
Alternatively you can create a collection by implementing collection and give it a sum method. The custom collection could be a collection of primitives or non-primitives. toArray method can be implemented to convert the Collection to an Array if needed.