且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java将两个数组变成一个二维数组

更新时间:2023-11-30 15:39:16

您可以创建一个 Pair 类,如下所示:

You can create a Pair class, like this:

public class Pair<K, V> {
    public K key;
    public V value;

    public Pair() {}

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }
}

然后创建一个 Pairs 数组.我已经有一段时间没有使用Java了,所以我可能在这里犯了一些错字,但这就是这个想法:

And then you create an array of Pairs. I have not worked in Java for a while, so I might have committed some typos here, but this is the idea:

Pair<char, int> result[] = new Pair<char, int>[keys.length];

for (int i = 0; i < keys.length; i++) {
    result[i] = new Pair<char, int>(keys[i], values[i]);
}