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

相关内容

热门资讯

透视计算"poker... 您好,pokernow辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
透视ai"we po... 1、透视ai"we poker辅助器v3.3"详细辅助可靠技巧(固有是有挂)。2、we poker辅...
透视挂透视"有没有人... 透视挂透视"有没有人wepoker"详细辅助必胜教程(切实是真的有挂)关于有没有人wepoker机制...
透视有挂"哈糖大菠萝... 透视有挂"哈糖大菠萝有挂吗"详细辅助wepoke教程(固有真的有挂);支持2-10人实时对战,虚拟庄...
透视好友房"wepo... 透视好友房"wepoker钻石怎么看底牌"详细辅助安装教程(原来存在有挂);致您一封信;亲爱wepo...
透视ai"poker... 您好,pokerrrr2辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视神器"德州真人透... 透视神器"德州真人透视脚本"详细辅助透视教程(竟然真的是有挂);致您一封信;亲爱德州真人透视脚本玩家...
透视透视"xpoke... 透视透视"xpoker怎么作弊"详细辅助黑科技教程(先前存在有挂)关于xpoker怎么作弊机制的,其...
透视代打"hhpok... 透视代打"hhpoker德州透视"详细辅助必备教程(确实是有挂);1分钟了解详细教程(微信 1367...
透视安装"xpoke... 透视安装"xpoker怎么作弊"详细辅助科技教程(本来有挂);玩家必备必赢加哟《136704302》...