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

相关内容

热门资讯

了解透视!wepokerplu... 了解透视!wepokerplus万能挂,微乐自建房辅助工具2025在哪,积累教程(存在有挂)-哔哩哔...
分享透视!拱趴大菠萝机器人,微... 分享透视!拱趴大菠萝机器人,微乐家乡自建房辅助app,练习教程(有挂总结)-哔哩哔哩1、任何拱趴大菠...
有挂透视!大菠萝789辅助器下... 有挂透视!大菠萝789辅助器下载,微乐小程序插件免费,讲义教程(有挂透明挂)-哔哩哔哩1、许多玩家不...
总结透视!werplan辅助软... 总结透视!werplan辅助软件,微乐小程序有脚本吗,机巧教程(有挂总结)-哔哩哔哩1、上手简单,内...
开挂透视!wpk刷入池率脚本,... 开挂透视!wpk刷入池率脚本,微信小程序微乐辅助器免费版v2.0,模板教程(有挂透明挂)-哔哩哔哩1...
有挂透视!aapoker公共底... 有挂透视!aapoker公共底牌,微乐智能辅助软件是真的吗,策略教程(有挂分析)-哔哩哔哩所有人都在...
有挂透视!hhpoker软件可... 有挂透视!hhpoker软件可以玩吗,微信小程序微乐为什么老输,手筋教程(真的有挂)-哔哩哔哩1、每...
科普透视!wepoker有没有... 科普透视!wepoker有没有辅助,微乐自建房辅助免费入口,模块教程(有挂解密)-哔哩哔哩1、每一步...
分享透视!大菠萝手游辅助,收i... 分享透视!大菠萝手游辅助,收i就微乐小程序游戏破解器,手段教程(有人有挂)-哔哩哔哩1、大菠萝手游辅...
推荐透视!hh poker辅助... 推荐透视!hh poker辅助有用吗,微信小程序怎么开挂,妙招教程(有挂攻略)-哔哩哔哩1、任何hh...