Hi, in which cases should I use arrays instead of ...
# getting-started
a
Hi, in which cases should I use arrays instead of lists ?
e
performance, but we are talking about special corner cases
r
Pretty much just interop with legacy APIs that require Arrays (very common in Java APIs)
Generally you don't want to be building new kotlin APIs that force you to use Arrays
Except ByteArray, that's still pretty common for performance
a
Should I use them for DoubleArrays and IntArrays ?
r
Again, depends on the application, how large you expect them to be and how much performance matters. If it's something like ARGB pixel values for image processing an array might make sense (although that may also be enforced by interop if you're using an image processing library)
a
Okay I see, thanks !
a
Working with primitives and arrays together happens often because usually if you're working with primitives instead of higher level data elements you're doing it for performance reasons in the first place
r
The general rule I've always gone by is
If you don't know if you need an array, you don't need an array
I know it's rather self-fulfilling, but it's worked pretty well for me
😂 1
1
a
For a game where I do some big calculations on coordinates or something and I store a lot of numbers in a list, should I use then an array ?
e
if the size is fixed and known a priori, you may use it
a
Okay it's very specific I see
r
For something like that, I would use an appropriate dedicated math library and the abstractions it provides. They tend to be quite well optimized.
If your goal is to just hold on to some data for a while, a list is probably a better option. It's useful to not think of arrays and lists as different implementations of the same data structure, but instead as fundamentally different data structures with different use cases.