The problem in a nutshell:
- You start with an array of length 4.
- At this moment, the array contains 4 zeros, that is:
[0, 0, 0, 0]
- You add 4, 3, and 5. These operations update the contents of the array to
[4, 3, 5, 0]. - .
[0, 3, 4, 5]. [0, 5, 3, 4], , sort . - , 1, 3,
[0, 5, 3, 1].
, , size, sort [3, 4, 5, 0], 1 [3, 4, 5, 1]. , [1, 3, 4, 5], 1, , 0.
:
- ,
private static int i = 0; private int size = 0;. i . size . static, . sort. , . , array.size, size. ? size - Scrap, add insert.
:
add insert add.remove delete. , , , ( )
.
Scrap pq :
[0, 0, 0, 0]
:
pq.add(4);
pq.insert(3);
pq.add(5);
:
[4, 3, 5, 0]
.
:
pq.sort();
:
[0, 5, 3, 4]
. , . . :
pq.insert(1);
:
[0, 5, 3, 1]
, , , . , . , .