5 ms·
In Java you do the exact same reallocation dance as append does behind the scenes when using arrays.
by wbl 6y ago
In Java you do the exact same reallocation dance as append does behind the scenes when using arrays.
- int_19h 6y agoJava standard library does that, not you directly. The issue here is not performance, but rather ergonomics. For example, in Go, you can forget to assign the result of append to the variable, and it'll even work most of the time (because there was still some unused capacity in the array, so there was no need for reallocation).
- wbl 6y agoWhich method? Note im talking about the language level array, not ArrayList. Also this was years ago.
- int_19h 6y agoJava arrays don't have add() at all - they're fixed-size once allocated. ArrayList etc do have add(), and they implement it by re-allocating the backing array once capacity is exceeded. In practice, you'd use the ArrayList anyway. I don't think it's worthwhile comparing Go and Java "language-only", because the standard library is as much a part of the language definition as the fundamental syntax; indeed, what goes where is largely arbitrary. E.g. maps in Go are fundamental, but the primary reason is that they couldn't be implemented as a library in Go with proper type safety, due to the lack of generics.