In my code, I have an array that has about 1000 elements :
Object[] arr = new Object[1000];
After my array is populated (the whole array or just partially), I need to reinitialize it. From what I know, I have two choices : to initialize it by new keyword, or to iterate over it and set each element to null. I think first approach is best than second, but also I’m waiting for your thoughts.
Any links or articles on this topic are welcome.
Thanks in advance
Agree with your first choice use
arr = new Object[1000]and don’t loop it.Also the use of new Object[1000] doesn’t create 1000 objects it only makes a “placeholder” for 1000 objects so it’s a very cheap operation.
And if you know you will populate all 1000 objects you can just use the array as is without reinitializing it.