将两个字符串数组合并到元素对列表

时间:2015-08-18 17:32:05

标签: java arrays guava

我有两个不同大小的数组:

added_items[1, 2, 3]
deleted_items[3, 4]

我的目标是将这些数组转换为以下对的列表:

list {
    added: 1, deleted 3
    added: 2, deleted 4
    added: 3, deleted: null
}

如果您使用guava,apache库等以最简单的方式实现此目的,以避免为每个等设置过多的自定义逻辑,那将会很好。

我真的很感激任何答案。

2 个答案:

答案 0 :(得分:2)

    int max = added_items.length > deleted_items.length ? added_items.length : deleted_items.length;
    for (int i = 0; i < max; i++) {
        Integer added = (i < added_items.length) ? added_items[i] : null;
        Integer deleted = (i < deleted_items.length) ? deleted_items[i] : null;
        pairs.add(new Pair(added, deleted));
    }

答案 1 :(得分:-2)

String[] MERGE = ArrayUtils.addAll(added_items, deleted_items);

或尝试:

import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;

concat(asList(added_items), asList(deleted_items))