How to find the index of an element in an int array?

前端 未结 19 1398
南方客
南方客 2020-11-27 02:56

How can I find an index of a certain value in a Java array of type int?

I tried using Arrays.binarySearch on my unsorted array, it only som

19条回答
  •  星月不相逢
    2020-11-27 03:20

    /**
         * Method to get the index of the given item from the list
         * @param stringArray
         * @param name
         * @return index of the item if item exists else return -1
         */
        public static int getIndexOfItemInArray(String[] stringArray, String name) {
            if (stringArray != null && stringArray.length > 0) {
                ArrayList list = new ArrayList(Arrays.asList(stringArray));
                int index = list.indexOf(name);
                list.clear();
                return index;
            }
            return -1;
        }
    

提交回复
热议问题