For instance, we have an arrayList,
There are a couple ways we can use to empty an array, so let's discuss them all.
This code will set the variable arrayList to a new empty array. This is perfect if you don't have references to the original array arrayList anywhere else because it will actually create a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable arrayList.
This is also the fastest solution.
This code sample shows the issue you can encounter when using this method:
The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.
The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.
This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.
Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4.