简体   繁体   中英

Store unknown amount of ints in array without arraylist or asking for amount of inputs

I'm curious as to how one would take an unknown amount of, let's just say ints of user input, and store them in an array? I know ArrayLists was designed for this, but let's say we can't use them nor can we ask the user of how many times they plan on entering inputs.

There is a class ResizableDoubleArray in one the Statistic Package of Appache Commons.

The nice thing is that it uses primitives, instead of objects. This needs much less memory (interesting for application with huge arrays.

Similar example: IntArrayList.java

They all work like ArrayList: if limit is reached, a new array is allocated which is eg 30% larger. fast System.arraycopy is used to copy the array content to new array.

See also Code design: performance vs maintainability

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM