我是靠谱客的博主 陶醉小甜瓜,这篇文章主要介绍数组元素查找(查找指定元素第一次在数组中出现的索引),现在分享给大家,希望可以做个参考。

public static void main(String[] args) {
        /*String [] ac = {"sche"," ","1123","we"};
        String a = "we";
        for(int i=0;i< ac.length;i++){
            if(ac[i]==a){
                System.out.println("索引位置是:"+i);
                break;
            }
            
        }*/
        /*
         *         查找数组中指定元素第一次出现的索引值。
                int[] arr = {98,23,16,35,72};
                查找23在数组中的索引值。
         */
    
                System.out.println(getIndex(23));
            }
        
            private static int getIndex(int x) {
                int[] arr = {98,23,16,35,72};
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] == x) {
                        return i;
                    }
                }
                return -1;  //若数组中没有则返回-1
            }
        
       

最后

以上就是陶醉小甜瓜最近收集整理的关于数组元素查找(查找指定元素第一次在数组中出现的索引)的全部内容,更多相关数组元素查找(查找指定元素第一次在数组中出现内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部