In Java we have toArray() method which will return Object[]. If I want to return another data type I need to pass it to parameters like this
SomeClass.SomeList.toArray(new AnotherClass[0]);
What does 0 mean in this case?
new AnotherClass[0] will create an array with 0 elements.
SomeList.toArray(...) will first check if the array is big enough to fit the elements and will create a new array of the desired size if it is not large enough.
You might prefer to do someList.toArray(new AnotherClass[someList.size()])
This way you don't create a zero element array and then throw it away.
But as @Rogue suggested, this does not perform better than new AnotherClass[0] (see https://shipilev.net/blog/2016/arrays-wisdom-ancients)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With