【问题标题】:(Java) Populate a 2d array with contents from another 2d array AND a map(Java) 使用来自另一个二维数组和地图的内容填充二维数组
【发布时间】:2017-10-03 16:01:00
【问题描述】:

我有这张地图:

static Map<String, String> myMap = new HashMap<>();

static {
    myMap.put("param1","param11");
    myMap.put("param2","param22");
}

我有这个二维数组:

Object[][] objArr1 = new Object[][]{
             {"1", 1},
             {"2", 2}
    };

我想将上面的“合并”到另一个二维数组中:

Object[][] objArr3 = new Object[][];

所以 objArr3 的结果内容是(不分先后):

 {"1", 1, "param1","param11"},
 {"1", 1, "param2","param22"},

 {"2", 2,"param1","param11"},
 {"2", 2,"param2","param22"

我知道我可能需要

new Object[objArr1 * myMap.size()][4];

但无法创建适当的嵌套 for 循环来执行此操作。我尝试结合herehere 的解决方案,但无济于事。 非常感谢任何帮助。

【问题讨论】:

  • 尝试过,但都是胡言乱语
  • 我们需要看到这些尝试才能为您提供帮助。请仔细阅读我提供的链接,以了解您现在应该做什么。

标签: java multidimensional-array


【解决方案1】:

这应该可行。
它遍历数组的所有行,以及映射的所有键/值对,将它们组合成一个新行。然后将每一行放入输出数组中。

static Map<String, String> map1 = new HashMap<>();

static {
    map1.put("param1","param11");
    map1.put("param2","param22");
}

static Object[][] array1 = new Object[][]{
            {"1", 1},
            {"2", 2}
};

public static void main (String[] args) throws java.lang.Exception
{
    // the new array has precisely N*M rows
    // where N is the number of rows in the input array
    // and M is the number of entries in the map
    Object[][] newArray = new Object[array1.length * map1.size()][4];
    int index = 0;
    for(int row=0;row<array1.length;row++)
    {
        for(Map.Entry<String, String> en : map1.entrySet())
        {
            Object[] newRow = new Object[4];
            newRow[0] = array1[row][0];
            newRow[1] = array1[row][1];
            newRow[2] = en.getKey();
            newRow[3] = en.getValue();
            newArray[index] = newRow;
            index++;
        }
    }
}

如果我然后输出数组,我得到

1 1 参数1 参数11
1 1 参数2 参数22
2 2 参数1 参数11
2 2 参数2 参数22

【讨论】:

  • 谢谢。新数组[索引] = 新行;是我错过的关键部分
  • 实际上,这并不能完全产生问题中提到的 objArr3 的预期内容。
  • 我忘记在行上索引 array1。我已经更改了代码。这只是一个轻微的修改。我添加了我的输出。
【解决方案2】:

我需要一个Object[][] 作为TestNg 的@DataPovider 的最终产品,但由于数组的大小固定,我无法解决问题。因此,我使用列表列表进行所有操作,并作为最后一步将其转换为二维数组:

public class main {

static Map<Object, Object> myMap = new HashMap<>();

static {
     myMap.put("1","1");
     myMap.put("2","2");
     myMap.put("3","3");
}


public static void main(String[] args){

    List<List<Object>> list = asList(
                         asList("param1", "param11"),
                         asList("param2", "param22")
                       );

    Object[][] finaList = mergeListWithMap(list, myMap);

    System.out.println(Arrays.deepToString(finaList));

}

private static Object[][] mergeListWithMap(List<List<Object>> list, Map<Object, Object> myMap) {
    List<List<Object>> newList = new ArrayList<>();

    for(List<Object> o : list){

         for(Map.Entry<Object, Object> en : myMap.entrySet()){
                List<Object> nl = new ArrayList<>(o); // copy original containing "param<x>", "param<xx>"
                nl.add(en.getKey());       // append               
                nl.add(en.getValue());     // append
                newList.add(nl);           // add final result to new list to be returned
         }
    }

    // Convert list of lists -> 2d-array
    Object[][] finaList = newList.stream()
            .map(l -> l.stream().toArray(Object[]::new))
            .toArray(Object[][]::new);

    return finaList;
}


private static <T> List<T> asList(T ... items) {
    List<T> list = new ArrayList<>();
    for (T item : items) {
        list.add(item);
    }
    return list;
}

}

输出:

 [param1, param11, 1, 1]
 [param1, param11, 2, 2]
 [param1, param11, 3, 3]
 [param2, param22, 1, 1]
 [param2, param22, 2, 2]
 [param2, param22, 3, 3]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2013-09-16
    • 2015-01-24
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多