Reminder about usage:
FixedLengthQueue prefixQueue = new FixedLengthQueue(4);
System.out.println("Queue with 0 elements: "+prefixQueue);
prefixQueue.add("element-one");
System.out.println("Queue with 1 element: "+prefixQueue);
prefixQueue.add("element-two");
System.out.println("Queue with 2 elements: "+prefixQueue);
prefixQueue.add("element-three");
System.out.println("Queue with 3 elements: "+prefixQueue);
prefixQueue.add("element-four");
System.out.println("Queue with 4 elements: "+prefixQueue);
prefixQueue.add("element-five");
System.out.println("Queue with 4 elements: "+prefixQueue);
prefixQueue.add("element-six");
System.out.println("Queue with 4 elements: "+prefixQueue);
The output of the above would be:
Queue with 0 elements:
Queue with 1 element: element-one
Queue with 2 elements: element-one element-two
Queue with 3 elements: element-one element-two element-three
Queue with 4 elements: element-one element-two element-three element-four
Queue with 4 elements: element-two element-three element-four element-five
Queue with 4 elements: element-three element-four element-five element-six
Note that this data structure does not guarantee any specified ordering of values except that if findKth(i) is called for 0 <= i < size(), for an object of multiplicity m stored in this MultiSet, findKth will return that object m times.
public class TestMultiSet { public static void main(String[] args) { MultiSet t = new MultiSet(); t.add("Washington G."); t.add("Jefferson T."); t.add("Lincoln A."); t.add("Roosevelt T."); t.add("Reagan R."); System.out.println("Items in order:"); for (int i = 0; i < t.size(); i++) { System.out.println(t.findKth(i)); } System.out.println(); t.add("Washington G."); t.add("Jefferson T."); t.add("Lincoln A."); t.add("Roosevelt T."); t.add("Reagan R."); System.out.println("Items in order:"); for (int i = 0; i < t.size(); i++) { System.out.println(t.findKth(i)); } System.out.println(); for (int i = 0; i < 50; i++) { t.add("Lincoln A."); } //adding an item more than once at a time probably won't //be useful for the Markov Chain project, but a data structure //like MultiSet should probably include a method for doing so t.add("Lincoln A.",50); System.out.println("Number of items stored:"); System.out.println(t.numEntries()); System.out.println(); ArrayList a = t.values(); System.out.println("Items actually stored:"); for (int i = 0; i < a.size(); i++) { System.out.println(a.get(i)+" has multiplicity "+t.getMultiplicity(a.get(i))); } System.out.println(); System.out.println("Out of bounds items k=-1 and k=1001:"); System.out.println(t.findKth(-1)); System.out.println(t.findKth(1001)); } }
As an alternative to the StringTokenizer, you may use the
String.split()
method to parse input.
String text = "This text\t contains \n\t whitespace\r\r\r\r\r."; String[] textSplit = text.split("\\s+"); for (int i = 0; i < textSplit.length; i++) { System.out.println(textSplit[i]); }produces the following output:
This text contains whitespace .Recall that \\ parses as a single backslash since it is Java's escape character; the argument given to split means match as a delimiter one or more ("+") whitespace characters ("\s").