java 问题
importjava.util.Enumeration;importcom.sun.org.apache.xalan.internal.xsltc.runtime.Has...
import java.util.Enumeration;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class TestHashtable
{
public static void main(String [] args)
{
Hashtable numbers = new Hashtable();
numbers.put(new MyKey("zhangsan",18),new Integer(1));
numbers.put(new MyKey("lisi",15),new Integer(2));
numbers.put(new MyKey("wangwu",20),new Integer(3));
Enumeration e = numbers.keys();
while(e.hasMoreElements())
{
MyKey key = (MyKey)e.nextElement();
System.out.print(key.toString() + "=");
System.out.println(numbers.get(key).toString());
}
}
}
class MyKey
{
private String name;
private int age;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return new String(name + "," + age);
}
public boolean equals(Object obj)
{
if(name.equals(obj.name) && age == obj.age)
return true;
else
return false;
}
public int hashCode()
{
return name.hashCode() + age;
}
}
if(name.equals(obj.name) && age == obj.age) 这行有错误
Exception in thread "main" zhangsan,18=java.lang.Error: Unresolved compilation problems:
obj.name cannot be resolved or is not a field
obj.age cannot be resolved or is not a field
at api.MyKey.equals(TestHashtable.java:39)
at com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable.get(Hashtable.java:168) 展开
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class TestHashtable
{
public static void main(String [] args)
{
Hashtable numbers = new Hashtable();
numbers.put(new MyKey("zhangsan",18),new Integer(1));
numbers.put(new MyKey("lisi",15),new Integer(2));
numbers.put(new MyKey("wangwu",20),new Integer(3));
Enumeration e = numbers.keys();
while(e.hasMoreElements())
{
MyKey key = (MyKey)e.nextElement();
System.out.print(key.toString() + "=");
System.out.println(numbers.get(key).toString());
}
}
}
class MyKey
{
private String name;
private int age;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return new String(name + "," + age);
}
public boolean equals(Object obj)
{
if(name.equals(obj.name) && age == obj.age)
return true;
else
return false;
}
public int hashCode()
{
return name.hashCode() + age;
}
}
if(name.equals(obj.name) && age == obj.age) 这行有错误
Exception in thread "main" zhangsan,18=java.lang.Error: Unresolved compilation problems:
obj.name cannot be resolved or is not a field
obj.age cannot be resolved or is not a field
at api.MyKey.equals(TestHashtable.java:39)
at com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable.get(Hashtable.java:168) 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询