FeiXuhust
BAN USEREncapsulation is the mechanism which ensures the safe and effective access of Classes or objects. For any outside objects which messages are sent from, the only things they need to take care is the interface of target object, while they dont care how does target object implement these interface. The target objects also hold some their own data and methods that is invisible for outside world, which improves the stability of the program.
- FeiXuhust February 02, 2011<pre lang="java" line="1" title="CodeMonkey47575" class="run-this"> public void inside_out_shuffle(int[] array, int[] source)
{
int i;
for (i=0;i<array.length;i++)
{
Random sequence = new Random();
int j=sequence.nextInt(i+1);
array[i]=array[j];
array[j]=source[i];
}
}</pre><pre title="CodeMonkey47575" input="yes">
</pre>
<pre lang="java" line="1" title="CodeMonkey12975" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey12975" input="yes">
</pre>
Assume we have a array that has a length 10 for this sampling. Just do Reservoir Sampling for every ten numbers from the stream. For example, collect first 10 numbers of the stream and store them into source[10], what we are looking for is a array[10] which is the random shuffle copy of source[10]. Then we can take a random access of array[10] to fetch a number. Do the same thing to following numbers in this stream.
- FeiXuhust February 02, 2011
"Virtual" keywords indicate that these functions might be overriden in the children classes. And this virtual function can not be directly called by base class.
- FeiXuhust February 02, 2011