How to get index of value in array?

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

    You could just use a for loop

    String[] array = { "Designation1", "Designation2", "Designation3" };   
     Scanner kb=new Scanner(System.in);
        String input=kb.next();
        int index;
        for(int i=0;i<array.length;i++)
        {
           if(array[i].equalsIgnoreCase(input))
         index=i;
        }
    
    0 讨论(0)
  • 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. ☺

    0 讨论(0)
  • 2021-01-26 08:15

    Consider using List instead of array (of just wrap your array in List). This way you will have access to method like indexOf(element) which will return index of first founded element, of -1 if no element in array was found.

    String[] array = { "Designation1", "Designation2", "Designation3" };
    List<String> list = Arrays.asList(array);
    
    System.out.println(list.indexOf("Designation2")); //prints 1
    System.out.println(list.indexOf("foo"));          //prints -1
    
    0 讨论(0)
  • 2021-01-26 08:21

    There are no direct search method for array so you need to convert it to list first

    String[] array = { "Designation1", "Designation2", "Designation3" };
    assert Arrays.asList(array).indexOf("Designation2") == 1;
    assert Arrays.asList(array).indexOf("Anything else") == -1;
    

    Do not forget that -1 mean 'not found'

    Or you can sort it and use binarySearch

    0 讨论(0)
  • 2021-01-26 08:23

    You can use ArrayUtils from Apache Commons.

    String[] array = { "Designation1", "Designation2", "Designation3" };
    
    System.out.println(ArrayUtils.indexOf(array, "Designation1"));//0
    System.out.println(ArrayUtils.indexOf(array, "Designation2"));//1
    System.out.println(ArrayUtils.indexOf(array, "Designation3"));//2
    
    0 讨论(0)
  • 2021-01-26 08:25

    You can loop over the Strings in the array and find the index for which the String matches what you are looking for.

    int index = -1;
    for (int i=0; i<array.length;i++) {
        if (array[i].equals(value)) {
            index = i;
            break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题