如何在Java中合并两个数组? [英] How to merge two arrays in Java?

查看:65
本文介绍了如何在Java中合并两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不是连接而是合并两个数组,以便它们成为名称/值对数组.

Its not to concatenate but to merge two arrays so that they become array of name value pairs.

firstarray = ["a","aa","aaa"]
secondarray = ["b","bb","bbb"]
result = [["a","b"],["aa","bb"],["aaa","bbb"]]

最好的方法是什么?

推荐答案

在Java中:

public static int [][] concat(int a[], int b[]) {
    int res[][] = new int[a.length][2];
    if (a.length != b.length) {
        throw new IllegalArgumentException("lenght are not equal, cannot perform");
    }
    for (int i = 0; i < a.length; i++) {
        res[i][0] = a[i];
        res[i][1] = b[i];
    }
    return res;
}

这篇关于如何在Java中合并两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆