MAP中保存的是String,写一个方法,把Map转换成String二维数组
3个回答
展开全部
如果一下代码看不懂可以询问
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();
}
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();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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]);
}
}
}
}
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]);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道你这个具体要求,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询