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.

debug why happening?

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

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -