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

相关内容

热门资讯

透视辅助!aapoker透视插... 透视辅助!aapoker透视插件(透视)插件(都是有挂)1、该软件可以轻松地帮助玩家将aapoker...
透视代打!aapoker怎么选... 透视代打!aapoker怎么选牌(透视)透视脚本下载(确实有挂)1、完成aapoker怎么选牌的残局...
透视代打!aapoker透视方... 透视代打!aapoker透视方法(透视)透视方法(真是是真的有挂)亲,关键说明,aapoker透视方...
透视有挂!aapoker辅助软... 透视有挂!aapoker辅助软件合法吗(透视)辅助(真是存在有挂)1、aapoker辅助软件合法吗透...
透视黑科技!aapoker透视... 透视黑科技!aapoker透视脚本(透视)透视脚本(本来是真的有挂)1、aapoker透视脚本系统规...
透视辅助!aapoker脚本(... 透视辅助!aapoker脚本(透视)辅助(好像存在有挂)1、aapoker脚本透视辅助简单,aapo...
透视实锤!aapoker透视方... 透视实锤!aapoker透视方法(透视)ai插件(都是是真的有挂)1、上手简单,内置详细流程视频教学...
透视教学!aapoker辅助工... 透视教学!aapoker辅助工具安全吗(透视)真的假的(原来真的有挂)1、透视教学!aapoker辅...
透视插件!aapoker透视脚... 透视插件!aapoker透视脚本入口(透视)免费透视脚本(切实存在有挂)在进入aapoker透视脚本...
透视存在!aapoker怎么拿... 透视存在!aapoker怎么拿好牌(透视)免费透视脚本(果然真的有挂)1、进入游戏-大厅左侧-新手福...