Java 为什么我的变量似乎丢失了我最近添加的内容?
我试图将RGB值添加到一个名为Java 为什么我的变量似乎丢失了我最近添加的内容?,java,key-value,Java,Key Value,我试图将RGB值添加到一个名为 HashMap<Integer,Couple> basicProvince 在这个方法之后,我调用下面的下一个方法,它应该使用这个变量中的信息,并在 c.setBasicRGB(e.getValue().getColor()); public void giveCountyBasicRGB() { String name = ""; System.out.println("give county basicRGB");
HashMap<Integer,Couple> basicProvince
在这个方法之后,我调用下面的下一个方法,它应该使用这个变量中的信息,并在
c.setBasicRGB(e.getValue().getColor());
public void giveCountyBasicRGB() {
String name = "";
System.out.println("give county basicRGB");
for (Entry<Integer, Couple> e : basicProvince.entrySet()) {
System.out.println("key = " + e.getKey() + "\t value= " + e.getValue().getName() + " " + e.getValue().getColor());
}
for (County c : countyList) {
name = c.getName();
for (Entry<Integer, Couple> e : basicProvince.entrySet()) {
if (name.equalsIgnoreCase(e.getValue().getName())) {
System.out.println("color " + e.getValue().getColor() + " name " + e.getValue().getName());
// here you can see the line that retrieves the info
c.setBasicRGB(e.getValue().getColor());
}
}
if (c.getBasicRGB() == null) {
System.out.println("Warning: " + c.getName() + " does not have a basic rgb");
}
}
}
尽管我刚刚在第一个方法中添加了这些信息
感谢您抽出时间,如果有些事情不够清楚,我深表歉意。如果我正确理解您的代码,变量“e”似乎是for循环的局部变量,当您尝试将其作为参数传递给SetBasicRGB函数时,该值将消失。
也许使用在for循环之外定义的另一个变量 如果您提供了一个简短但完整的程序来演示该程序,而不是多个代码片段,那么帮助您会更容易。您能提供您的
Couple
类吗?如何返回getColor()
和setBasicRGB()
key = 8 value= breifne java.awt.Color[r=126,g=24,b=4]
c.setBasicRGB(e.getValue().getColor());
public void giveCountyBasicRGB() {
String name = "";
System.out.println("give county basicRGB");
for (Entry<Integer, Couple> e : basicProvince.entrySet()) {
System.out.println("key = " + e.getKey() + "\t value= " + e.getValue().getName() + " " + e.getValue().getColor());
}
for (County c : countyList) {
name = c.getName();
for (Entry<Integer, Couple> e : basicProvince.entrySet()) {
if (name.equalsIgnoreCase(e.getValue().getName())) {
System.out.println("color " + e.getValue().getColor() + " name " + e.getValue().getName());
// here you can see the line that retrieves the info
c.setBasicRGB(e.getValue().getColor());
}
}
if (c.getBasicRGB() == null) {
System.out.println("Warning: " + c.getName() + " does not have a basic rgb");
}
}
}
for (Entry<Integer, Couple> e : basicProvince.entrySet()) {
System.out.println("key = " + e.getKey() + "\t value= " + e.getValue().getName() + " " + e.getValue().getColor());
}
e.getValue().getColor()