Map映射.java
/**
* get(Object Key) 方法返回指定键所映射的值
* put(K key, V value) 方法来为集合添加数据
*/
public class CityMap {
public static Map<String, String> model = new LinkedHashMap();
static {
model.put("北京", new String[]{"北京市"});
model.put("上海", new String[]{"上海市"});
model.put("天津", new String[]{"天津市"});
model.put("重庆", new String[]{"重庆市"});
}
}
public String[] getProvince() {
Map<String, String> map = CityMap.model;
Set<String> st = map.keySet();
String[] province = st.toArray();
return province;
}
public String[] getCity(String selectedProvince) {
Map<String, String> map = CityMap.model;
String[] arrCity = map.get(selectedProvince);
return arrCity;
}
TreeSet 数组.java
/**
* TreeSet 集合属于 Set 集合的子类, Set 集合不允许有重复的元素, TreeSet() 类的 add() 方法添加元素:
*/
public class TreeSet {
public static void main(String[] args) {
TreeSet<Integer> tSet = new TreeSet<Integer>();
Random ran = new Random();
int count = 0;
while(count < 10) {
boolean succeed = tSet.add(ran.nextInt(100));
if (succeed) {
count++;
}
}
int size = tSet.size();
Integer[] arr = new Integer[size];
tSet.toArray(arr);
System.out.println("生成的不重复随机数组如下: ");
for (int value : arr) {
System.out.print(value + " ");
}
}
}
遍历ArrayList.java
import java.util.ArrayList;
import java.util.List;
public class LocalTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
/** 初始化 List 集合 **/
list.add("abc");
list.add("def");
System.out.println("foreach 遍历集合: ");
for(String string : list) {
System.out.println(string);
}
String[] strs = new String[list.size()];
list.toArray(strs);
System.out.println("foreach 遍历数组: ");
for (String s : strs) {
System.out.println(s);
}
}
}