簡體   English   中英

(Java) 用另一個二維數組和地圖中的內容填充二維數組

[英](Java) Populate a 2d array with contents from another 2d array AND a map

我有這張地圖:

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 循環來執行此操作。 我試圖結合這里這里的解決方案,但無濟於事。 非常感謝任何幫助。

這應該有效。
它遍歷數組的所有行和映射的所有鍵/值對,將它們組合在一個新行中。 然后將每一行放入輸出數組中。

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

我需要一個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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM