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免费透视脚本(透视)好像是真的有挂(详细辅助2025新版总结)1)hhpok...
必备科技!hhpoker是真的... 必备科技!hhpoker是真的假的,wpk作弊最怕三个东西,新2025教程(有挂透视);1分钟了解详...
透视讲解!wepoker透视器... 透视讲解!wepoker透视器免费(透视)总是是真的有挂(详细辅助wepoke教程)小薇(透视辅助)...
玩家必知教程!wpk透视辅助下... 玩家必知教程!wpk透视辅助下载,wejoker辅助器要钱玩吗,必赢教程(有挂技巧);建议优先通过w...
透视黑科技!wepokerpl... 透视黑科技!wepokerplus辅助(透视)总是是有挂(详细辅助必备教程)一、AI软件牌型概率发牌...
必知教程!约局吧辅助器,wep... 必知教程!约局吧辅助器,wepoker代打辅助,扑克教程(有挂透视);建议优先通过wepoker代打...
透视了解!aapoker发牌逻... 透视了解!aapoker发牌逻辑(透视)果然存在有挂(详细辅助攻略教程)辅助器中分为三种模型:软件透...
免费测试版!aapoker怎么... 免费测试版!aapoker怎么提高中牌率,hhpoker买挂,安装教程(有挂技巧);aapoker怎...
透视模拟器!德普之星透视辅助软... 透视模拟器!德普之星透视辅助软件激活码(透视)都是存在有挂(详细辅助AI教程)1、德普之星透视辅助软...
玩家必用!菠萝辅助器免费版的特... 玩家必用!菠萝辅助器免费版的特点,we poker辅助器下载,详细教程(有挂透视);致您一封信;亲爱...