[英]My array all contain "zeros" when I copy data from hashMap object to my array
I am iterating through my hashMap object to fill up my array with either 0 or 1;我正在遍历我的 hashMap 对象以用 0 或 1 填充我的数组; I have a condition that when a record in my hashMap object equals "positive" then write "1" to my array else write "zero" to my array.
我有一个条件,当我的 hashMap 对象中的记录等于“正”时,然后将“1”写入我的数组,否则将“零”写入我的数组。
When I print the content of my array;当我打印数组的内容时; all cells contain zeros, which is not the case.
所有单元格都包含零,但事实并非如此。
Can you please advise why???你能告诉为什么吗???
int index = 0;
if ( (!map.isEmpty()) && (index < PTFindings.length ) ) {
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
if(entry.getValue().equals("Positive")) {
PTFindings[index] = 1;
index++;
}
else if (entry.getValue().equals("Negative") ) {
PTFindings[index] = 0;
index++;
}
Log.d("map values", entry.getKey() + ": " +
entry.getValue().toString());
System.out.println("PTFindings at index " + index + " : " + PTFindings[index]);
}
}
You are comparing strings with two equals sign (==), don't do that.您正在比较带有两个等号 (==) 的字符串,不要这样做。 Use strings .equals() method eg
使用字符串 .equals() 方法例如
if(entry.getValue().equals("Positive"))
You can find more on this here How do I compare strings in Java?您可以在此处找到更多相关信息如何比较 Java 中的字符串?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.