寻找这段代码的解决方案,请完成程序 [英] Lookin for a solution of this code, , , complete the program please

查看:109
本文介绍了寻找这段代码的解决方案,请完成程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Employee{

	String name;
	double salary;
	
	public Employee(String name, double salary){
		this.name = name;
		this.salary = salary;
	}
	
	public int hashCode(){
		//normal implementation using name and salary
	}
	
	public boolean equals(){
		//normal implementation using name and salary
	}
	
}

Employee e1 = new Employee("emp1",2500);
Employee e2 = new Employee("emp2",2600);
Employee e3 = new Employee("emp3",2700);

Map<Employee,Double> map = new HashMap<>();
map.put(e1,4000.00);
map.put(e2,5000.00);
map.put(e3,3000.00);

e1 = new Employee("emp4",2800);

double salary1 = map.get(e1);

System.out.println(salary1);

e2.name = "emp5";

double sal2 = map.get(e2);

System.out.println(sal2);





我的尝试:





What I have tried:

lookin for a solution of this code,,,complete the program please

推荐答案

Quote:

e1 = new Employee("emp4",2800);
double salary1 = map.get(e1);
System.out.println(salary1);



假设你的 hashCode 等于实现是正确的, Map 中没有匹配的键。据我所知,这应该生成 NullPointerException ,因为你试图将 null 的引用取消装箱值类型。



java - 使用HashMap的NullPointerException< Integer,Double>当输入为双倍时 - Stack Overflow [ ^ ]




Assuming your hashCode and equals implementations are correct, there is no matching key in the Map. As far as I can see, that should generate a NullPointerException, since you're trying to unbox a null reference to a value type.

java - NullPointerException with HashMap<Integer,Double> when input is double - Stack Overflow[^]

Quote:

e2.name = "emp5";
double sal2 = map.get(e2);
System.out.println(sal2);



您已更新现有对象的名称。结果将取决于 hashCode 函数的实现,以及 HashMap 类的实现。



假设 HashMap 类使用哈希码将对象映射到桶:

  • 如果新的哈希码映射到另一个桶,将不再找到该密钥,并且您将获得另一个 NullPointerException ;
  • 如果新的哈希码映射到同一个桶,则会找到密钥,结果会得到 5000 ;

  • You've updated the name of an existing object. The result will depend on the implementation of your hashCode function, and the implementation of the HashMap class.

    Assuming the HashMap class uses the hash code to map the objects into "buckets":

    • If the new hash code maps to a different bucket, the key will no longer be found, and you'll get another NullPointerException;
    • If the new hash code maps to the same bucket, the key will be found, and you'll get 5000 as the result;
    • 每当在执行Java应用程序期间多次在同一对象上调用它时,hashCode方法必须始终返回相同的整数...

      Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer...



      java - 可变对象和hashCode - 堆栈溢出 [ ^ ]


      这些不是我们可以提供的东西:我们我不知道代码的目的是什么,所以我们不知道它可能是什么那是错的!



      所以,这取决于你。

      幸运的是,你有一个工具可供你使用帮助您了解发生了什么:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



      放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


      对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
      THose are not things we can provide: we have no idea what the purpose of the code is, so we have no idea what it might be doing that is wrong!

      So, it's going to be up to you.
      Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

      Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

      Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


      你的大部分代码都在课堂之外定义。您尚未创建 main 方法来包含该类之外的所有语句。您有两种方法缺少 return 语句。使用Java编译器查找实际问题。
      Much of your code is outside of the class definition. You have not created a main method to contain all the statements beyond the class. You have two methods that are missing their return statements. Use the Java compiler to find the actual problems.


      这篇关于寻找这段代码的解决方案,请完成程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆