java - Queue has more items that I put in it -
in java program, initialized queue numbers 0 1000.
emptyframes = new priorityqueue<integer>(); (int = 0; < 1000; i++) { emptyframes.add(i); } system.out.println("debug");
however, when go in debug, there 1155
items in queue.
the indices greater 1000 related queue's capacity, rather size.
internally, priorityqueue
backed array of objects. when adding objects queue full backing array, queue expand array moderate amount calling grow
, have internal space (capacity) available future add
calls. avoids queue having expand array every time add
called, horribly inefficient.
private void grow(int mincapacity) { int oldcapacity = queue.length; // double size if small; else grow 50% int newcapacity = oldcapacity + ((oldcapacity < 64) ? (oldcapacity + 2) : (oldcapacity >> 1)); // overflow-conscious code if (newcapacity - max_array_size > 0) newcapacity = hugecapacity(mincapacity); queue = arrays.copyof(queue, newcapacity); }
code retrieved docjar.
Comments
Post a Comment