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

相关内容

热门资讯

日前"如何下载wpk... 日前"如何下载wpk透视版"hhpoker有没有辅助(好像是真的辅助安装)-哔哩哔哩1、每一步都需要...
有消息称"如何下载德... 有消息称"如何下载德普之星辅助软件"来玩app破解(确实存在有辅助辅助器)-哔哩哔哩1、如何下载德普...
透视神器"we-po... 透视神器"we-poker有人玩吗"we-poker软件(都是是有辅助插件)-哔哩哔哩1、点击下载安...
透视辅助"大菠萝免费... 透视辅助"大菠萝免费辅助"wepoker怎么设置透视(竟然存在有辅助插件)-哔哩哔哩wepoker怎...
透视肯定"约局吧德州... 透视肯定"约局吧德州有挂吗"wepoker有透视吗(确实真的有辅助辅助器)-哔哩哔哩暗藏猫腻,小编详...
透视好友房"wepo... 透视好友房"wepoker游戏的安装教程"hhpoker有没有辅助(其实是真的辅助安装)-哔哩哔哩1...
透视苹果版"wepo... 透视苹果版"wepoker辅助器激活码"德州透视脚本(竟然存在有辅助插件)-哔哩哔哩1、wepoke...
不少玩家反映"hhp... 不少玩家反映"hhpoker辅助靠谱吗"we poker免费辅助器(竟然真的有辅助app)-哔哩哔哩...
据报道"佛手大菠萝有... 据报道"佛手大菠萝有挂吗"wepoker是不是有人用挂(都是真的是有辅助神器)-哔哩哔哩;1、wep...
据公告内容"hhpo... 据公告内容"hhpoker开挂教程"wepoker新号好一点吗(都是是真的辅助辅助器)-哔哩哔哩1、...