How to get index of value in array?

前端 未结 7 1007
無奈伤痛
無奈伤痛 2021-01-26 07:37

I have an array like this :

String[] array = { \"Designation1\", \"Designation2\", \"Designation3\" };

If user put \"Designation2\" as input th

7条回答
  •  甜味超标
    2021-01-26 08:14

    In order to do this task, you can use two arrays, one for key names and ones for their values. Simply search for the key in the first array, get the index of the key name and use it to get the value from the second array. It's not the most efficient, and this is probably not the best answer, but it works for me.

    Just make sure the indexes in the arrays line up, for example:

    {"my","three","keys"};
    {"My","Three","Values"};
    

    In this case, the key/value setup would be;

    my/My three/Three keys/Values

    In your case, you don't need to use the value array, just use the index.

    Also try using ArrayList instead of arrays, as you can use ArrayList.indexOf(key) to get the index of key in the ArrayList.

    Hope this helps you and others with this problem. ☺

提交回复
热议问题