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

相关内容

热门资讯

4分钟辅助!wepoker底牌... 4分钟辅助!wepoker底牌透视,epoker底牌透视,经验教程(证实有挂)1、上手简单,内置详细...
第9分钟辅助!wepoker有... 第9分钟辅助!wepoker有脚本吗,wepoker轻量版透视方法,经验教程(真的有挂)1、每一步都...
第四分钟辅助!hhpoker德... 第四分钟辅助!hhpoker德州牛仔视频,hhpoker透视脚本视频,经验教程(有挂详情)1、hhp...
第4分钟辅助!wepoker一... 第4分钟辅助!wepoker一直输的号能继续打吗,wepoker怎么看牌型,策略教程(有挂细节)1、...
八分钟辅助!wepoker轻量... 八分钟辅助!wepoker轻量版透视系统,hhpoker辅助挂下载,法门教程(确实有挂)1、该软件可...
第2分钟辅助!werplan免... 第2分钟辅助!werplan免费挂下载,wepoker免费脚本,妙计教程(证实有挂)1)werpla...
五分钟辅助!哈糖大菠萝助手,h... 五分钟辅助!哈糖大菠萝助手,hhpoker软件安装包,教材教程(发现有挂)1、上手简单,内置详细流程...
第3分钟辅助!拱趴大菠萝作弊方... 第3分钟辅助!拱趴大菠萝作弊方法,xpoker辅助怎么用,手册教程(有挂透明挂)一、拱趴大菠萝作弊方...
两分钟辅助!poker辅助器免... 两分钟辅助!poker辅助器免费安装,hhpoker有没有外挂,大纲教程(有挂助手)1、很好的工具软...
第三分钟辅助!wpk透视怎么安... 第三分钟辅助!wpk透视怎么安装,hhpoker透视脚本视频,演示教程(有挂技巧)暗藏猫腻,小编详细...