java词典

import java.util.Iterator;

public interface DictionaryInterface<K, V>  {
	public V add(K key, V value);

	public V remove(K key);

	public V getValue(K key);

	public boolean contains(K key);

	public Iterator<K> getKeyInterator();

	public Iterator<V> getValueInterator();

	public boolean isEmpty();

	public boolean isFull();

	public int getSize();

	public void clear();
}
import java.io.Serializable;
import java.util.Currency;
import java.util.Iterator;

import javax.naming.spi.DirStateFactory.Result;

public class SortedArrayDictionary<K extends Comparable<? super K>, V> implements
		DictionaryInterface<K, V>, Serializable {

	private Entry<K, V>[] dictionary;
	private int size;
	private static final int DEFAULT_CAPACITY = 5;

	public SortedArrayDictionary() {
		this(DEFAULT_CAPACITY);
	}

	public SortedArrayDictionary(int initialCapacity) {
		dictionary = new Entry[initialCapacity];
		size = 0;
	}

	private class Entry<S, T> implements Serializable {
		private S key;
		private T value;

		public Entry(S key, T value) {
			super();
			this.key = key;
			this.value = value;
		}

		public S getKey() {
			return key;
		}

		// 不需要设置查找键的方法
		// public void setKey(S key) {
		// this.key = key;
		// }
		public T getValue() {
			return value;
		}

		public void setValue(T value) {
			this.value = value;
		}

	}

	@Override
	public V add(K key, V value) {
		// TODO Auto-generated method stub
		V result = null;
		int keyIndex = locationIndex(key);
		if (keyIndex < size && key.equals(dictionary[keyIndex].getKey())) {
			result = dictionary[keyIndex].getValue();
			dictionary[keyIndex].setValue(value);

		} else {
			if (isArrayFull())
				doubleArray();
			makeRoom(keyIndex);
			dictionary[keyIndex] = new Entry<K, V>(key, value);
			size++;
		}
		return result;
	}

	@Override
	public V remove(K key) {
		// TODO Auto-generated method stub
		V result = null;
		int keyIndex = locationIndex(key);
		if ((keyIndex < size) && key.equals(dictionary[keyIndex].getKey())) {
			result = dictionary[keyIndex].getValue();
			removeArrayElement(keyIndex);
			size--;
		}
		return result;
	}

	private void removeArrayElement(int keyIndex) {
		int i = 0;
		for (i = keyIndex; i < size - 1; i++)
			dictionary[i] = dictionary[i + 1];
		dictionary[i] = null;
	}

	@Override
	public V getValue(K key) {
		// TODO Auto-generated method stub
		V result = null;
		int keyIndex = locationIndex(key);
		if ((keyIndex < size) && key.equals(dictionary[keyIndex].getKey())) {
			result = dictionary[keyIndex].getValue();
		}
		return result;
	}

	@Override
	public boolean contains(K key) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterator<K> getKeyInterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterator<V> getValueInterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return size==0;
	}

	@Override
	public boolean isFull() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
        for(int i=0;i<dictionary.length;i++)
        	dictionary[i]=null;
	}

	private boolean isArrayFull() {
		return size == dictionary.length;
	}

	private void doubleArray() {
		System.out.println("数组已满,需要扩展");
		Entry<K, V>[] old = dictionary;
		int oldSize = dictionary.length;
		dictionary = new Entry[2 * oldSize];
		for (int i = 0; i < oldSize; i++)
			dictionary[i] = old[i];
	}

	private int locationIndex(K key) {
		int index = 0;
		while ((index < size) && key.compareTo(dictionary[index].getKey()) > 0)
			index++;
		return index;
	}

	private void makeRoom(int keyIndex) {
		for (int i = size; i > keyIndex; i--)
			dictionary[i] = dictionary[i - 1];
	}

	public static void main(String[] argv) {
		SortedArrayDictionary<String, Integer> dic = new SortedArrayDictionary<String, Integer>();
		dic.add("aa", 1);
		dic.add("bb", 2);
		System.out.println(dic.getValue("bb"));
		System.out.println(dic.add("bb", 3));
		dic.add("cc", 2);
		System.out.println(dic.getValue("bb"));
		dic.add("dd", 4);
		dic.add("ee", 5);
		System.out.println(dic.getValue("aa"));
		dic.add("ff", 6);
		System.out.println(dic.dictionary.length);
		System.out.println(dic.getValue("ff"));
		System.out.println(dic.remove("cc"));
		System.out.println(dic.getValue("cc"));
	}

}

import java.io.Serializable;
import java.util.Currency;
import java.util.Iterator;

import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler;

import org.w3c.dom.Node;

public class SortedLinkedDictionary<K extends Comparable<? super K>, V>
		implements DictionaryInterface<K, V>, Serializable {

	private Node firstNode;
	private int size;

	public SortedLinkedDictionary() {
		firstNode = null;
		size = 0;
	}

	private class Node {
		private K key;
		private V value;
		private Node next;

		public Node(K key, V value) {
			super();
			this.key = key;
			this.value = value;
		}

		public Node(K key, V value, Node next) {
			super();
			this.key = key;
			this.value = value;
			this.next = next;
		}

		public K getKey() {
			return key;
		}

		// public void setKey(K key) {
		// this.key = key;
		// }
		public V getValue() {
			return value;
		}

		public void setValue(V value) {
			this.value = value;
		}

		public Node getNext() {
			return next;
		}

		public void setNext(Node next) {
			this.next = next;
		}

	}

	@Override
	public V add(K key, V value) {
		// TODO Auto-generated method stub
		V result = null;
		Node currentNode = firstNode;
		Node beforeNode = null;
		while ((currentNode != null) && key.compareTo(currentNode.getKey()) > 0) {
			beforeNode = currentNode;
			currentNode = currentNode.getNext();
		}
		if ((currentNode != null) && key.equals(currentNode.getKey())) {
			result = currentNode.getValue();
			currentNode.setValue(value);
		} else {
			Node newNode = new Node(key, value);
			size++;
			if (beforeNode == null) {
				newNode.setNext(firstNode);
				firstNode = newNode;
			} else {
				newNode.setNext(currentNode);
				beforeNode.setNext(newNode);
			}
		}

		return result;
	}

	@Override
	public V remove(K key) {
		// TODO Auto-generated method stub
		V result = null;
		Node currenNode = firstNode;
		Node beforeNode = null;
		while (currenNode != null && key.compareTo(currenNode.getKey()) > 0) {
			beforeNode = currenNode;
			currenNode = currenNode.getNext();
		}
		if (currenNode != null && key.equals(currenNode.getKey())) {
			result = currenNode.getValue();
			size--;
			if (currenNode == firstNode)
				firstNode = firstNode.getNext();
			else {
				beforeNode.setNext(currenNode.getNext());
			}

		} else {
			System.out.println("找不到该元素");
		}

		return result;
	}

	@Override
	public V getValue(K key) {
		// TODO Auto-generated method stub
		V result = null;
		Node currenNode = firstNode;
		while (currenNode != null && key.compareTo(currenNode.getKey()) > 0) 
			currenNode = currenNode.getNext();
		if(currenNode!=null&&key.equals(currenNode.getKey()))
			result=currenNode.getValue();
		return result;
	}

	@Override
	public boolean contains(K key) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterator<K> getKeyInterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterator<V> getValueInterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isFull() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] argv) {
		SortedLinkedDictionary<String, Integer> dic=new SortedLinkedDictionary<String, Integer>();
		dic.add("aa", 1);
		dic.add("bb", 2);
		System.out.println(dic.getValue("bb"));
		System.out.println(dic.add("bb", 3));
		dic.add("cc", 2);
		System.out.println(dic.getValue("bb"));
		dic.add("dd", 4);
		dic.add("ee", 5);
		System.out.println(dic.getValue("aa"));
		dic.add("ff", 6);
		System.out.println(dic.size);
		System.out.println(dic.getValue("ff"));
		System.out.println(dic.remove("cc"));
		System.out.println(dic.getValue("cc"));
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值