Posted by ½ÅÀǼ· on August 19, 1997 at 13:09:20:
In Reply to: [Áú¹®] Hashtable¿¡¼... posted by ÃÖÀ翵 on August 19, 1997 at 01:25:31:
Á¦°¡ ¿Ã·Á³õÀ¸½Å ¼Ò½ºÄڵ带 ¾à°£ ¼ÕºÁ¼ Å×½ºÆ®Çغ¸´Ï ÀߵǴõ±º¿ä.
ÁÖ·Î °íÄ£ ºÎºÐÀº input¿¡¼ '\n'À» '\r'·Î °íÃÆ°í, ±×·¡µµ ÀÚ²Ù error°¡ ³ª¼ º¸´Ï
equals() ¸Þ¼Òµå¿¡¼ charAt ¸Þ¼Òµå ¼öÇà½Ã OutofBound°¡ ³ª¿À´õ¶ó±¸¿ä
±×·¡¼ ¾Æ·¡ÀÇ ¼Ò½º¿Í °°ÀÌ ¼öÁ¤Çß½À´Ï´Ù.
µµ¿òÀÌ µÇ½Ã±æ...
----------------------------------------------
import java.util.Hashtable;
class man_info {
String name;
int n;
public man_info(String s) {
name = s;
n = s.length();
// System.out.println("C = "+n);
}
public int hashCode() {
return (int)name.charAt(0);
}
public boolean equals(Object compare) {
man_info temp = (man_info)compare;
System.out.println("this " + this.name + this.n);
System.out.println("temp " + temp.name + temp.n);
System.out.println("n = " + n);
for(int i = 0; i < (temp.n - 1); i++) {
System.out.println(i);
if(this.name.charAt(i) != temp.name.charAt(i)) {
return false;
}
}
return true;
}
} // end of class man_info
class hash {
public static void main(String args[]) throws java.io.IOException {
Hashtable hashtable = new Hashtable();
man_info man[] = new man_info[5];
char search;
man[0] = new man_info("tiger"); // ¾ê³×µéÀÇ length´Â ¼ø¼öÇÑ ±ÛÀÚµéÀÇ
man[1] = new man_info("elephant"); // ±æÀ̸¸ ¸®Åϵ˴ϴÙ. tiger = 5 ...
man[2] = new man_info("javaman");
man[3] = new man_info("mazinga");
man[4] = new man_info("jaeyoung");
for(int i = 0; i < 5; i++)
hashtable.put(man[i], man[i]);
System.out.print("Input characters for the name : "); // printlnÀ» print·Î-- º¸±â ÁÁÀ¸¶ó°í
StringBuffer stringtemp = new StringBuffer();
// System.out.println(stringtemp.length());
while((search = (char)System.in.read()) != '\r') {
// System.out.println(search);
stringtemp.append(search);
// System.out.println(stringtemp.length()); // ±×·±µ¥ ¿©±â¼ÀÇ ±æÀÌ´Â
} // 1ÀÌ ´õ ¸¹ÀÌ ³ª¿ÀÁÒ. tig = 4....
System.out.println(stringtemp.length()); // test Çغ¸´Ï µü ¸Â¾Æ¿ä..
String s = new String(stringtemp.toString()); // ¹°·Ð StringÀ¸·Î ¹Ù²ãµµ ¸¶Âù°¡Áö..
man_info searched = new man_info(s);
// System.out.println("l = " + s.length());
man_info result = (man_info)hashtable.get(searched);
if(result == null)
System.out.println("Nothing..."); // Ç×»ó result°¡ ³ÎÀ̶ó´Â ±º¿ä....°ÅÂü..
else
System.out.println(result.name);
}
}