Java - get element position in array

后端 未结 4 1856
谎友^
谎友^ 2021-01-03 20:47

I\'m familiar with the ways I can get an element position in array, especially the ones showed here: Element position in array

But my problem is I can\'t figure out

4条回答
  •  一整个雨季
    2021-01-03 21:46

    Use indexOf:

    int index = listPackages.indexOf(current_package);
    

    Note that you shouldn't generally use == to compare strings - that will compare references, i.e. whether the two values are references to the same object, rather than to equal strings. Instead, you should call equals(). That's probably what was going wrong with your existing code, but obviously using indexOf is a lot simpler.

提交回复
热议问题