MAP中保存的是String,写一个方法,把Map转换成String二维数组
发布网友
发布时间:2023-04-09 08:52
我来回答
共3个回答
热心网友
时间:2023-09-14 13:03
如果一下代码看不懂可以询问
Map<String,String> map = new HashMap<String, String>();
for (int i = 0; i < 5; i++) {
map.put(""+i, "map"+i);
}
for (int i = 0; i < map.size(); i++) {
System.out.println(map.get(""+i));
}
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
String[][] ss = new String[map.size()][2];
for (int i = 0; i < map.size(); i++) {
ss[i][0] = it.next();
ss[i][1] = map.get(ss[i][0]);
}
for (int i = 0; i < ss.length; i++) {
for (int j = 0; j < ss[i].length; j++) {
System.out.print(ss[i][j]+"\t");
}
System.out.println();
}
热心网友
时间:2023-09-14 13:03
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
String[][] twoDarray = new String[map.size()][2];
String[] keys = map.keySet().toArray(new String[map.size()]);
String[] values = map.values().toArray(new String[map.size()]);
for (int row = 0; row < twoDarray.length; row++) {
twoDarray[row][0] = keys[row];
twoDarray[row][1] = values[row];
}
for (int i = 0; i < twoDarray.length; i++) {
for (int j = 0; j < twoDarray[i].length; j++) {
System.out.println(twoDarray[i][j]);
}
}
}
}
热心网友
时间:2023-09-14 13:04
不知道你这个具体要求,