1.把 二维数组(Array)转换成 Map
/*
* 把 二维数组(Array)转换成 Map
*/
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
public class ArrayToMap {
public static void main(String[] args) {
String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },
{ "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };
//循环遍历数组中的每个元素
for (int i = 0; i < countries.length; i++) {
for (int j = 0; j < countries[i].length; j++) {
System.out.print((String)countries[i][j]);
System.out.print(" ");//输出空格
//将数组中的元素输出
}
System.out.println();//回车,转入下一行
}
Map countryCapitals = ArrayUtils.toMap(countries); //ArrayUtils数组工具类的使用!
System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));
System.out.println("Capital of France is " + countryCapitals.get("France"));
}
}
2.输出结果: