1. traverse the list set:
@ Test
Public void testlist (){
List list = new arraylist ();
List. Add ("111 ");
List. Add ("222 ");
List. Add ("333 ");
List. Add ("444 ");
For (Object OBJ: List ){
String STR = (string) OBJ;
System. Out. println (STR );
}
}
2. traversal of the map set: the map set contains countless key-value pairs. The map set is first converted into a set to generate an iterator.
@ Test
Public void testmap (){
Map map = new hashmap ();
Map. Put (1, "AA ");
Map. Put (2, "BB ");
Map. Put (3, "cc ");
// Obtain the value by retrieving the Set set of the key.
Set set1 = map. keyset ();
Iterator it1 = set1.iterator ();
// While Traversal
While (it1.hasnext ()){
Int key = (integer) it1.next ();
String value = (string) map. Get (key );
System. Out. println ("key:" + key + "value:" + value );
}
// For Loop Traversal
For (Object OBJ: set1 ){
Integer key = (integer) OBJ;
String value = (string) map. Get (key );
System. Out. println ("key:" + key + "value:" + value );
}
// Obtain the key and value values of the map set by traversing the Set set of the object.
Set set2 = map. entryset ();
Iterator it2 = set2.iterator ();
// While Traversal
While (it2.hasnext ()){
Map. Entry entry = (entry) it. Next ();
Integer key = (integer) entry. getkey ();
String value = (string) entry. getvalue ();
System. Out. println ("key:" + key + "value:" + value );
}
// For Loop Traversal
For (Object OBJ: set2 ){
Map. Entry entry = (entry) OBJ;
Integer key = (integer) entry. getkey ();
String value = (string) entry. getvalue ();
System. Out. println ("key:" + key + "value:" + value );
}
}
3. jdk1.5 for can only be set to values, but cannot be assigned values. to modify the list element, you must use
". Set ('', '')" method to modify
@ Test
Public void testarrays (){
// Arrays. aslist (T... t) returns a fixed size supported by the specified array
You cannot use the add method of list to add values.
List list1 = arrays. aslist (1, 2, 3, 4, 5, 6 );
For (Object OBJ: list1 ){
Integer temp = (integer) OBJ;
Temp = 100;
System. Out. println (OBJ + ",");
}
For (Object OBJ: list1 ){
System. Out. println (OBJ + ","); list1.set (1,200 );
System. Out. println (OBJ + ",");
}
}