Java Reference
In-Depth Information
// Max objects is set to 1000
static double []
container= new double [1000];
// Stack in FIFO order
static void add( double a)
{ if (freePlace < 1000)
{ container [ freePlace]=a;
freePlace++;
}
}
// Process in FIFO order
static double process ()
if (freePlace lastProcessed > 1)
{ lastProcessed++;
return container [ lastProcessed ];
else
return
1.0; // code for impossible to process
}
public static void main( String [ ] arg )
System . out . println ( "Queue demo:" );
add(3.0) ; add(5.0) ; add(7.0) ;
System . out . println ( process () ) ;
System . out . println ( process () ) ;
System . out . println ( process () ) ;
System . out . println ( process () ) ;
System . out . println ( process () ) ;
}
}
Running the above code yields:
Queue demo:
3.0
5.0
7.0
-1.0
-1.0
As it can be seen from the output, queues ensure that first-in elements are
indeed first-out: That is, queues respect the order of arrival. Note that in this
implementation all functions are static functions attached to the program class.
Namely, they are class functions that access the buffer array defined as a static
class array static double[] container .
Search WWH ::




Custom Search