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方法查找该元素在第二个数组中的索引值,并将结果输出到控制台。

相关内容

热门资讯

透视经验!约局吧能不能开挂(透... 透视经验!约局吧能不能开挂(透视)真是是真的辅助神器(哔哩哔哩)1、首先打开约局吧能不能开挂辅助器下...
透视策略!佛手大菠萝有挂吗(透... 您好,佛手大菠萝有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
透视手册!wepoker怎么挂... 透视手册!wepoker怎么挂底牌(透视)竟然存在有辅助辅助器(哔哩哔哩)1、许多玩家不知道wepo...
透视经验!wepoker透视脚... 透视经验!wepoker透视脚本安卓(透视)好像有辅助app(哔哩哔哩)wepoker透视脚本安卓破...
透视法子!wepoker游戏的... 透视法子!wepoker游戏的安装教程(透视)好像真的有辅助安装(哔哩哔哩)1)wepoker游戏的...
透视方式!hhpoker一直输... 透视方式!hhpoker一直输有挂吗(透视)果然真的是有辅助插件(哔哩哔哩)1、hhpoker一直输...
透视指南书!德州私人局脚本(透... 透视指南书!德州私人局脚本(透视)原来真的是有辅助安装(哔哩哔哩)1、点击下载安装,德州私人局脚本脚...
透视指南书!wepoker究竟... 透视指南书!wepoker究竟有没有透视(透视)本来真的是有辅助平台(哔哩哔哩)1、完成wepoke...
透视指引!wepoker免费钻... 透视指引!wepoker免费钻石(透视)确实是真的辅助安装(哔哩哔哩)暗藏猫腻,小编详细说明wepo...
透视秘籍!pokemmo手机脚... 透视秘籍!pokemmo手机脚本辅助器(透视)都是存在有辅助神器(哔哩哔哩);1、让任何用户在无需p...