📄 testdictionary.java
字号:
import java.io.*;
import java.util.Random;
public class TestDictionary
{
public TestDictionary()
{
testDictionary();
}
public void testDictionary()
{
System.out.println("\nStart ArrayedBasicDictionary");
ArrayedBasicDictionary floatDictionary = new ArrayedBasicDictionary(10);
floatDictionary.insert(new Float(5.7));
floatDictionary.insert(new Float(9.3));
floatDictionary.insert(new Float(-4.8));
floatDictionary.insert(new Float(12.7));
System.out.println("Dictionary contents " + floatDictionary.toString());
Float f = new Float(9.3);
if (floatDictionary.has(f))
floatDictionary.delete(f);
floatDictionary.delete(new Float(12.7));
int i = ((Float) floatDictionary.obtain(new Float(5.7))).intValue();
System.out.println("\ni has value " + i);
System.out.println("Dictionary contents " + floatDictionary);
ArrayedBasicDictionary udInt = new ArrayedBasicDictionary(10);
udInt.insert(new Integer(5));
udInt.insert(new Integer(9));
udInt.insert(new Integer(-4));
udInt.insert(new Integer(12));
udInt.insert(new Integer(0));
udInt.insert(new Integer(7));
udInt.insert(new Integer(6));
System.out.println(udInt.toString());
udInt.delete(new Integer(7));
udInt.delete(new Integer(5));
if (udInt.has(new Integer(6)))
udInt.delete(new Integer(6));
udInt.insert(new Integer(2));
udInt.insert(new Integer(1));
udInt.insert(new Integer(9));
System.out.println(udInt.toString());
/* Create a dictionary and insert three BetterBasicboxes. */
ArrayedBasicDictionary boxDictionary = new ArrayedBasicDictionary(10);
BetterBasicBox bbox1 = new BetterBasicBox(1, 11, 21);
boxDictionary.insert(bbox1);
BetterBasicBox bbox2 = new BetterBasicBox(2, 12, 22);
boxDictionary.insert(bbox2);
BetterBasicBox bbox3 = new BetterBasicBox(3, 13, 23);
boxDictionary.insert(bbox3);
/* If the dictionary has a box with dimensions 2, 12, and 22, then delete it. */
BetterBasicBox bbox4 = new BetterBasicBox(2, 12, 22);
if (boxDictionary.has(bbox4))
boxDictionary.delete(bbox4);
/* Obtain the box from the dictionary with dimensions 1, 11, and 21. */
Object obj;
obj = boxDictionary.obtain(new BetterBasicBox(1, 11, 21));
BetterBasicBox bbox5 = (BetterBasicBox) obj;
/* Change the definition of equality in the dictionary to compare
boxes by reference. */
boxDictionary.compareObjectReferences();
if (boxDictionary.has(bbox5))
boxDictionary.delete(bbox5);
/* The next three statements do not delete a box, as bbox6 references a new
box, so it will never be found in the dictionary using reference equality. */
BetterBasicBox bbox6 = new BetterBasicBox(3, 13, 23);
if (boxDictionary.has(bbox6))
boxDictionary.delete(bbox3);
System.out.println("\n" + boxDictionary.toString());
System.out.println("End ArrayedBasicDictionary");
System.out.println("\nStart ArrayedPKeyedBasicDictionary");
ArrayedPKeyedBasicDictionary udkBox = new ArrayedPKeyedBasicDictionary(10);
BasicBox box1 = new BasicBox(1, 11, 21);
BasicBox box2 = new BasicBox(2, 12, 22);
BasicBox box3 = new BasicBox(3, 13, 23);
BasicBox box4 = new BasicBox(4, 14, 24);
udkBox.insert(new Integer(box3.length), box3);
udkBox.insert(new Integer(box3.length), box3);
udkBox.insert(new Integer(box2.length), box2);
udkBox.insert(new Integer(box1.length), box1);
udkBox.insert(new Integer(box4.length), box4);
System.out.println("\n" + udkBox.toString());
udkBox.delete(new Integer(box2.length));
BasicBox box5 = (BasicBox)udkBox.obtain(new Integer(box1.length));
if (udkBox.has(new Integer(box5.length)))
udkBox.delete(new Integer(box5.length));
if (!udkBox.has(new Integer(box5.length)))
udkBox.insert(new Integer(box3.length), box3);
System.out.println("\n" + udkBox.toString());
if (udkBox.has(new Integer(box3.length)))
udkBox.delete(new Integer(box3.length));
if (!udkBox.has(new Integer(box3.length)))
udkBox.delete(new Integer(box3.length));
System.out.println("\n" + udkBox.toString());
ArrayedPKeyedBasicDictionary symbolTable = new ArrayedPKeyedBasicDictionary(10);
symbolTable.insert("x", new Integer(4));
symbolTable.insert("y", new Float(12.1));
symbolTable.insert("z", new Integer(9));
symbolTable.insert("temp", "Temp string");
System.out.println(symbolTable.toString());
System.out.println("The value of x is " + symbolTable.obtain("x"));
System.out.println("\nEnd ArrayedPKeyedBasicDictionary");
}
public static void main(String args[])
{
new TestDictionary();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -