It took a little extra work writing the lists as a static array, and I need to set a limit for how many objects that can enter the queues, but in the end it all turns out good. However, I'd like some feedback on the code itself. It is likely to be error-prone, and quite possibly there are several optimization tricks that can be done.
note: dont take synchronous issues in to mind right now. I didnt worry about it yet.
public class RenderList { public final static int DEFAULT_SIZE = 1000; private IRender[] objectList; public int nextIndex; public int highestIndex; public RenderList() { this(DEFAULT_SIZE); } public RenderList(int size) { objectList = new IRender[size]; nextIndex=0; highestIndex = -1; } /* registers an object to the list */ public void registerObject(IRender ob) { if( nextIndex>=objectList.length || highestIndex>=objectList.length) return; if(nextIndex==-1 || nextIndex>highestIndex) { objectList[++highestIndex]=ob; nextIndex=highestIndex+1; } else { objectList[nextIndex]=ob; // often the next index might also be free if(objectList[nextIndex+1]==null) { nextIndex++; } // otherwise, lets search for it. else nextIndex=getNextFreeIndex(); } } /* gets the index of an object. -1 if its not in the list */ public int memberArray(IRender ob) { for(int i=0;i<(highestIndex+1);i++) { if(objectList==null) continue; if(objectList.equals(ob)) return i; } return -1; } /* removes an object from the list */ public void removeObject(IRender ob) { int index=memberArray(ob); if(index==-1) { return; } objectList[index]=null; // if this index is less than nextIndex, // nextIndex becomes this index if(index<nextIndex || nextIndex==-1) nextIndex=index; // if the highest index is this index, or higher // than this index, reduce highest index if(highestIndex>=index) { while(highestIndex>=0) { if(objectList[highestIndex]==null) highestIndex--; else { if(nextIndex>(highestIndex+1)) nextIndex=(highestIndex+1); break; } } } } /* get a free index where a new object can be inserted */ public int getNextFreeIndex() { for(int i=0;i<highestIndex;i++) { if(objectList==null) return i; } return highestIndex+1; } /* retrieve an object from the list */ public IRender getObject(int i) { if(i<=objectList.length) { return objectList; } return null; } public void render(Graphics2D g) { for(int i=0;i<(highestIndex+1);i++) { if(objectList==null) continue; objectList.render(g); } } public int getListsize() { return objectList.length; }}