簡體   English   中英

當我將數據從 hashMap 對象復制到我的數組時,我的數組都包含“零”

[英]My array all contain "zeros" when I copy data from hashMap object to my array

我正在遍歷我的 hashMap 對象以用 0 或 1 填充我的數組; 我有一個條件,當我的 hashMap 對象中的記錄等於“正”時,然后將“1”寫入我的數組,否則將“零”寫入我的數組。

當我打印數組的內容時; 所有單元格都包含零,但事實並非如此。

你能告訴為什么嗎???

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]);
            }
        }

您正在比較帶有兩個等號 (==) 的字符串,不要這樣做。 使用字符串 .equals() 方法例如

if(entry.getValue().equals("Positive"))

您可以在此處找到更多相關信息如何比較 Java 中的字符串?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM