Re: Quick way to initialize array with all zeros



On Apr 1, 12:28 am, "Andrew Thompson" <andrewtho...@xxxxxxxxx> wrote:
On Mar 31, 11:59 pm, Patricia Shanahan <p...@xxxxxxx> wrote:

001 wrote:
I want to initialize a large array with 0's... do I have to use a for-loop
or is there some trick to accomplish this?

new int[n] is an all zero int array of size n. If you have an existing
array and need to reinitialize it to zero, use Arrays.fill.

I 'object' to such simplistic answers to what is
effectively a complex question. My first question
for the OP would be, what do you mean by 'quick'?

There are two potential meanings, AFAIU.
1) quick to code.
2) quick to run.

D'oh! In my (mild) irritation I forgot to
mention two important things.

1) Premature optimization

These types of questions smack of premature
optimization. Code for clarity and let the
JIT do its job until such times as a *profiler*
indicates a bottleneck based on the array
initialisation.

2) (Stressing) code clarity

The Array.fill() method would be preferable
for code clarity. Why code three lines, for
something that can be expressed in one?

Andrew T.

.



Relevant Pages

  • Re: casting PositionNormalColored[] to byte[]
    ... that pncs is just a pointer to the beginning of an array of structures. ... value, int offset, int count)!!! ... premature optimization is not a good thing! ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: return return new int[] { pounds, shilings, pence}; to main
    ... >> The function can then load this parameter directly or create a temporary ... > I would call that premature optimization, ... NRVO cannot be used effectively ... std::vectorarray; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: casting PositionNormalColored[] to byte[]
    ... I think that you're right about the premature optimization is a bad ... that pncs is just a pointer to the beginning of an array of structures. ... value, int offset, int count)!!! ...
    (microsoft.public.win32.programmer.directx.managed)