リニアサーチ

とりあえず定本を写経

public class LinearSearch {
	static private class Entry {
		int key;
		Object data;
		private Entry(int key, Object data)
		{
			this.key = key;
			this.data = data;
		}
	}
	final static int MAX = 100;
	Entry[] table = new Entry[MAX];
	int n = 0;
	
	public void add(int key, Object data)
	{
		if (n >= MAX){
			throw new IllegalStateException("too many data");
		}
		table[n++] = new Entry(key, data);
	}
	public Object search(int key)
	{
		int i = 0;
		while ( i < n) {
			if (table[i].key == key)
				return (table[i].data);
			i++;
		}
		return null;
	}
}	

public class Sample01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		LinearSearch table = new LinearSearch();
		table.add(1, "one");
		table.add(10, "ten");
		table.add(2, "two");
		
		String x;
		x = (String)table.search(10);
		if (x != null){
			System.out.println("Value = " +  x);
		}else{
			System.out.println("not found");
		}
	}

}