New Features of JDK 1.5 Loop

Source: Internet
Author: User
Tags set set

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 + ",");
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.