Java中集合的理解
集合是按照我的理解就是用来存放java类的对象,虽然说数组也可以,但是数组在定义的时候是需要长度的,我在不知道的数量情况下那么集合就是解决这个问题的,知识要点都画在图里面了

接下来是使用集合的一些典型代码
- ArrayList
import java.util.*;
public class testArrayList {
public static void main(String args[]){
ArrayList<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
for(String str:list){ //foreach循环,list是容器变量
System.out.println(str); //str是临时变量
}
}
}
- HashSet
import java.util.*;
public class testHashSet {
public static void main(String args[]){
HashSet set = new HashSet();
set.add("1");
set.add("2");
set.add("3");
Iterator it = set.iterator();//获取迭代器
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}
}
- HashMap
public class testHashMap {
public static void main(String args[]){
Map map = new HashMap();
map.put("1", "Jack");
map.put("2", "Rose");
Set keyset = map.keySet();
Iterator it = keyset.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
}
}
- Properties
public class testProperties {
public static void main(String args[]){
Properties p = new Properties();
p.setProperty("background-color", "red");
p.setProperty("language", "chinese");
Enumeration name = p.propertyNames();
while(name.hasMoreElements()){
String key = (String) name.nextElement();
String value = p.getProperty(key);
System.out.println(key+"="+value);
}
}
}