IndexOfcontains, etc. make the operation strange.

Asked 2 years ago, Updated 2 years ago, 113 views

//hm.get("name"): "1_01.wav"
//sound_list: ["1_00.wav","1_01.wav"]
//Value is as above.

Log.d("asdf", String.valueOf(sound_list.contains(hm.get("name"))));  //-1
Log.d("asdf", String.valueOf(sound_list.contains("1_01.wav")));   //2

Log.d("asdf", String.valueOf(sound_list.contains(hm.get("name"))));  //false
Log.d("asdf", String.valueOf(sound_list.contains("1_01.wav")));   //true

You are trying to get the location of a specific value in the ArrayList. Specific values were extracted as get from HashMap.

However, like the first case of the code above, it returns false if you put it in the contents, and -1 if you put it in indexOf.

However, like the second case of the code above, hm to the input value of contains or indexOf.If you insert a value such as get("name") directly, it will function normally.

What on earth is this??

android arraylist

2022-09-22 18:31

1 Answers

So you have to use generic.

In HashMap, get returns Object. I'm sure you expected String, but...

Please change it as below.

But use generic. That's one of the ways to reduce errors.

Log.d("asdf", String.valueOf(sound_list.contains((String)hm.get("name"))));


2022-09-22 18:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.