Ahh, I found it. I made the constructor private and looked at the errors. My binarySearch algorithm was creating an object to compare with the list. Is this how it's supposed to work?
1 2 3 4 5 6 7 8 9 10
| public int getIndex(int z) { Entity e = new Entity(new Vector(0,0,z));
int insertPos = Arrays.binarySearch(entities.toArray(new Entity[0]), e, zComparator); if (insertPos < 0) insertPos ^= 0xFFFFFFFF; return insertPos; } |
[EDIT]: I guess that's how it's supposed to work, I was just a bit surprised by the amount of objects that were created solely to run the binarySearch(..), well, I suppose it's not a problem.
What is the point of XOR-ing insertPos by -1 when it is less than 0?
As has been explain by others, I want the index position the object would take in the list regardless if it's actually found or not. (Arrays.binarySearch(..) returns an inverted value - 1 the object would take if it is not actually found.)
Ie instead of:
1 2
| if (insertPos < 0) insertPos = -insertPos - 1; |
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
[size=6pt]source:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#binarySearch%28java.util.List,%20java.lang.Object,%20java.util.Comparator%29[/size]