How to get index of value in array?

前端 未结 7 858
無奈伤痛
無奈伤痛 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:30

    You can do it like this.

    String userinput="Designation2";
    String[] array = { "Designation1", "Designation2", "Designation3" };
    int length=array.length();
    int index=0;
    for(int i=0;i<length;i++)
    {
        if(array[i].equals(userinput))
        {
             index=i;
             break;
        }
    
    }
    

    And index will give you the array key that user wants. Regards..

    0 讨论(0)
提交回复
热议问题