Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a real-life java example defining when it is better to use a one-dimensional array instead of an arraylist?

I am aware that:

In the case of choosing between a one-dimensional array and an arraylist, arraylists should always be the preferable choice with one main exception; when collecting primitive values AND efficiency is an issue. Since the ArrayList uses arrays internally, high performance (in terms of processing time and memory footprint) is similar to arrays when performing operations on Object types. However, ArrayList performance suffers when operating on primitive types, such as int or long, since ArrayList requires all elements to be wrapped in an Object (such as Integer or Long). While autoboxing reduces the amount of code required for wrapping and unwrapping, it does not remove the performance issues, as wrapper objects are still being created.

But I am searching for a real-life example where i would be collecting primitive values AND efficiency is an issue. Can you think of one?

like image 339
Danny Rancher Avatar asked Jan 31 '26 15:01

Danny Rancher


2 Answers

SCIENCE. It's very common to have one or more sensors collecting a fixed number of data values per time interval. E.g. think of hundreds of thermometers collecting the temperature every millisecond.

like image 89
Matthew Flaschen Avatar answered Feb 03 '26 05:02

Matthew Flaschen


Or maths, where you have a matrix or vector of doubles and wish to sum / aggregate them in some way. Here the unboxing overhead would be large with many values.

like image 29
daveb Avatar answered Feb 03 '26 04:02

daveb