简体   繁体   中英

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; 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.

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

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

You can find more on this here How do I compare strings in Java?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM