ArrayList<Integer>candidates=newArrayList<Integer>();//Dynamically resizable array list for allocation candidates (interleaved with index and memory size)
intcounter=0;//Counter for measuring unallocated memory
//Scan through memory block and get free blocks
for(inti=0;i<memoryBlock.length;i++)
{
//If position in memory block here is 0, iterate from that index and count up sequential 0's
if(memoryBlock[i]==0)
{
for(intj=i;j<memoryBlock.length-i;j++)
{
if(memoryBlock[j]==0)
{
counter++;
}
}
if(counter>=jobSize)
{
candidates.add(i);//Store index
candidates.add(counter);//Store size of free memory chunk
}
counter=0;
}
}
//Iterate through candidate sizes
worstSizeIndex=0;//Initialize best index to first spot in array list
worstSize=candidates.get(1).intValue();//Initialize bestSize to first space size in candidate
//Iterate through sizes and find the best fit
for(inti=1;i<candidates.size();i=i+2)
{
//If the current size is greater than the previous best size, make this the new best size
if(candidates.get(i).intValue()>worstSize)
{
worstSize=candidates.get(i+1).intValue();
worstSizeIndex=i-1;
}
}
//If the best size is less than the job size, run this again