Android Studio Java - 第一个数组显示第二个数组的索引值。
创始人
2024-08-16 18:30:10
0

下面是一个示例代码,演示如何显示第一个数组中每个元素在第二个数组中的索引值。

public class Main {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {2, 4, 6, 8, 10};

        for (int i = 0; i < array1.length; i++) {
            int index = findIndex(array1[i], array2);
            System.out.println("Element " + array1[i] + " is at index " + index + " in array2");
        }
    }

    public static int findIndex(int element, int[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1; // 如果元素不存在于数组中,则返回-1
    }
}

输出结果如下:

Element 1 is at index -1 in array2
Element 2 is at index 0 in array2
Element 3 is at index -1 in array2
Element 4 is at index 1 in array2
Element 5 is at index -1 in array2

在上述示例中,我们定义了findIndex方法来查找给定元素在数组中的索引。然后,我们使用循环遍历第一个数组的每个元素,并使用findIndex方法查找该元素在第二个数组中的索引值,并将结果输出到控制台。

相关内容

热门资讯

脚本透视!hhpoker真的假... 脚本透视!hhpoker真的假的,hhpoker德州挂真的有吗(透视)确实真的是有器(哔哩哔哩)1、...
透视数据!hh poker软件... 透视数据!hh poker软件,微乐陕西三代自建房挂,解迷脚本(有挂详情)-哔哩哔哩1、hh pok...
备受关注的!wepoker怎么... 备受关注的!wepoker怎么看牌型,微乐小程序辅助开发,解迷脚本(有挂方式)-哔哩哔哩1)wepo...
透视解迷!wepoker有透视... 透视解迷!wepoker有透视底牌吗,wepoker怎么挂底牌(透视)总是是有神器(哔哩哔哩)1、玩...
透视线上!pokemmo脚本最... 透视线上!pokemmo脚本最新版,微信小程序微乐挂如何获取,揭露脚本(有挂秘籍)-哔哩哔哩1、全新...
透视开挂!aapoker万能辅... 透视开挂!aapoker万能辅助器,aapoker辅助器怎么用(透视)果然真的是有方法(哔哩哔哩)该...
做出回应!hh poker辅助... 做出回应!hh poker辅助有用吗,微乐家乡app下载,了解脚本(有挂细节)-哔哩哔哩1)hh p...
透视曝光!德普辅助软件,如何下... 透视曝光!德普辅助软件,如何下载德普之星辅助软件(透视)一直是有脚本(哔哩哔哩)1、每一步都需要思考...
透视攻略!pokemmo辅助器... 透视攻略!pokemmo辅助器手机版下载,微乐自建房辅助神器怎么安装,教你脚本(有挂详情)-哔哩哔哩...
脚本有挂!微扑克微乐辅助,wp... 脚本有挂!微扑克微乐辅助,wpk透视辅助方法(透视)总是有攻略(哔哩哔哩)1、脚本有挂!微扑克微乐辅...