Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I clear an array?

array clear
0
Posted

How do I clear an array?

0

Ans : There is no method to clear an array to 0.0, 0, null, false, ‘\u0000’ etc. When you allocate an array, the elements are set to their default values, but that doesn’t help when you want to reuse an array. If you want to set the same array to the same set of values many times, create a template array. Fill it with the reset value, then use System.arraycopy() to copy it into the work array each time you need to set the work array.

0

There is no method to clear an array to 0.0, 0, null, false, ‘\u0000’ etc. When you allocate an array, the elements are set to their default values, but that doesn’t help when you want to reuse an array. If you want to set the same array to the same set of values many times, create a template array. Fill it with the reset value, then use System.arraycopy() to copy it into the work array each time you need to set the work array. • (Sect. 6) What is a fast way to set all elements of an array? I don’t want to use a template array. I would like to set all array elements to a given value without duplicating the (possibly large) array. Using a loop that does it one by one is probably 20 to 40 times slower than good old memset() in C. A fast way on many VM’s is to set the first byte of the array, then use System.arraycopy() repeatedly to fill the next byte, the next two bytes, the next four bytes, the next eight bytes, etc., and when you get past halfway, fill in the rest.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.