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

相关内容

热门资讯

关于透视!xpoker辅助神器... 关于透视!xpoker辅助神器!总是是真的有辅助攻略(有挂技巧)-哔哩哔哩1)xpoker辅助神器免...
分享透视!cloudpoker... 分享透视!cloudpoker作必弊!确实有辅助脚本(有挂秘笈)-哔哩哔哩1、许多玩家不知道clou...
揭露透视!竞技联盟透视!切实是... 揭露透视!竞技联盟透视!切实是真的有辅助软件(有挂技巧)-哔哩哔哩1、该软件可以轻松地帮助玩家将竞技...
关于透视!红龙poker有辅助... 关于透视!红龙poker有辅助吗!切实一直都是有辅助攻略(有挂秘籍)-哔哩哔哩1、下载好红龙poke...
详情透视!智星菠萝透视!本来一... 详情透视!智星菠萝透视!本来一直都是有辅助app(今日头条)-哔哩哔哩1、下载好智星菠萝透视脚本下载...
科普透视!pokernow辅助... 科普透视!pokernow辅助工具!原来存在有辅助方法(有挂工具)-哔哩哔哩1、很好的工具软件,可以...
曝光透视!pokemmo免费脚... 曝光透视!pokemmo免费脚本!都是是有辅助app(有挂方式)-哔哩哔哩1、pokemmo免费脚本...
了解透视!werplan透视挂... 了解透视!werplan透视挂!好像有辅助神器(有挂神器)-哔哩哔哩1、这是跨平台的werplan透...
专业透视!pokemmo修改器... 专业透视!pokemmo修改器手机版!真是是有辅助插件(真是有挂)-哔哩哔哩1、下载好pokemmo...
分享透视!epoker透视底牌... 分享透视!epoker透视底牌!切实真的有辅助插件(有挂秘诀)-哔哩哔哩1、实时epoker透视底牌...