大哥,你试过没呢,你所说的方法在里面都一样的呀,我这里都运行过了,没出任何问题
算了我还是直接把一个一个类贴出来好了
public class Memory {
private final int STR_SIZE = 9;
private final
int SIZE_INCREASE = 9;
private int
size = 0;
private Person[] persons = new Person[STR_SIZE];
public Memory() {
}
//扩展数组长度函数,假如数组长度不够,以SIZE_INCREASE扩展
private Person[] increaseStr(Person[] str, int increaseSize){
Person[] result = new Person[str.length+increaseSize];
for(int i=0;i<str.length;i++){
result[i]=str[i];
}
return result;
}
public void put(String name, String numberOne) {
if(size>=persons.length){
persons = increaseStr(persons,SIZE_INCREASE);
}
Person p=new Person();
p.setName(name);
p.setNumber(Integer.parseInt(numberOne));
persons[size] = p;
size++;
}
public String get(String name) {
int result=-1;
for(int i=0;i<size;i++){
if(persons[i].getName().equals(name)){
result=persons[i].getNumber();
break;
}
}
return new Integer(result).toString();
}
public int size(){
return size;
}
public void remove(String name) {
int j=0,i;
for(i=0;i<size;i++){
if(!persons[i].getName().equals(name)){
persons[j++] = persons[i];
}else{
}
}
if(i==j)
return;
persons[size-1]=null;
size--;
}
public void clear() {
for (int i = 0; i < size; i++) {
persons[i] = null;
}
size=0;
}
}
//封装了put join 36的name(join)和number(36)
public class Person {
private String name;
private int number;
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
protected int getNumber() {
return number;
}
protected void setNumber(int number) {
this.number = number;
}
}
import
import java.util.Scanner;
import java.util.StringTokenizer;
public class Hw03 {
public static void main(String[] args) throws IOException {
int i = 0, flag = 0;
String command, name, number; // To hold command
String[] array = new String[2];
Scanner keyboard = new Scanner(System.in);
//实例必须在循环体外,不然每次都生成一个新的,就没有用了
//假如要放在do里面,就必须把men设成单件模式
Memory mem = new Memory();
do {
System.out.print("Command? ");
command = keyboard.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(command,
"get put");
while (strTokenizer.hasMoreTokens()) {
array[i] = strTokenizer.nextToken();
i++;
}
i = 0;
name = array[0];
number = array[1];
if (command.startsWith("size"))
System.out.println("Currently remembering " + mem.size()
+ " things.");
else if (command.startsWith("help")) {
System.out.println("get
Retrieve an item by its key");
System.out.println("put
Store a <key,value> pair");
System.out.println("clear
Removes all pairs from the memory");
System.out.println("exit
Exit the program");
System.out.println("help
Display this text");
System.out.println("remove
Removes a given key from the memory");
System.out.println("size
Report the size of the memory");
}
else if (command.startsWith("exit"))
flag = 1;
else if (command.startsWith("put"))
mem.put(name, number);
else if (command.startsWith("get")) {
if (mem.get(name) == "-1")
System.out.println("That key gets null.");
else
System.out.println("That key gets " + mem.get(name));
}
else if (command.startsWith("remove"))
mem.remove(name);
else if (command.startsWith("clear")) {
mem.clear();
System.out.println("Memory has been cleared!");
}
} while (flag != 1);
}
}