Java – how does arraybuffer work in memory?
•
Java
I want to use arraybuffer in my program to save a list of numbers I want to use it as a queue Delete the first item in the list each time and use it Then I want to know that every time the first item is deleted, all the other numbers in the queue move one place I mean, can I read the item (0) again next time?
Solution
If you look at Scala source code or arraybuffer, you will see the following implementation of remove:
/** Removes the element on a given index position. It takes time linear in * the buffer size. * * @param n the index which refers to the first element to delete. * @param count the number of elements to delete * @throws Predef.indexoutofboundsexception if `n` is out of bounds. */ override def remove(n: Int,count: Int) { require(count >= 0,"removing negative number of elements") if (n < 0 || n > size0 - count) throw new indexoutofboundsexception(n.toString) copy(n + count,n,size0 - (n + count)) reduceToSize(size0 - count) }
Therefore, the deleted elements will no longer exist in the array, but in fact, deletion means copying all elements (shift in the definition), which will be slow for longer arrays
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码