List遍历的3种方法
import java. util. ArrayList;
import java. util. Iterator;
import java. util. List;
public class ListDemo1 {
public static void main ( String[ ] p) {
List list= new ArrayList ( ) ;
list. add ( "java1" ) ;
list. add ( "java2" ) ;
list. add ( "java3" ) ;
Iterator iterator = list. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
System. out. println ( iterator. next ( ) ) ;
}
System. out. println ( "======================" ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
System. out. println ( list. get ( i) ) ;
}
System. out. println ( "***********************" ) ;
for ( Object a: list) {
System. out. println ( a) ;
}
}
}
ArrayList遍历的3种方法
import java. util. ArrayList;
import java. util. Iterator;
public class ArrayListDemo3 {
public static void main ( String[ ] args) {
ArrayList countryList = new ArrayList ( ) ;
countryList. add ( "France" ) ;
countryList. add ( "USA" ) ;
countryList. add ( "India" ) ;
countryList. add ( "Spain" ) ;
countryList. add ( "England" ) ;
Iterator iter = countryList. iterator ( ) ;
while ( iter. hasNext ( ) ) {
System. out. println ( iter. next ( ) ) ;
}
for ( int i = 0 ; i < countryList. size ( ) ; i++ ) {
System. out. println ( countryList. get ( i) ) ;
}
for ( Object a: countryList) {
System. out. println ( a) ;
}
}
}
Map遍历的三种方法
import java. util. HashMap;
import java. util. Iterator;
import java. util. Map;
import java. util. Set;
public class MapDemo2 {
public static void main ( String[ ] qq) {
Map< String, Integer> m = new HashMap < String, Integer> ( ) ;
m. put ( "zhangsan" , 19 ) ;
m. put ( "lisi" , 49 ) ;
m. put ( "wangwu" , 19 ) ;
m. put ( "lisi" , 20 ) ;
m. put ( "hanmeimei" , null) ;
for ( Map. Entry< String, Integer> entry: m. entrySet ( ) ) {
System. out. println ( entry. getKey ( ) + "---->" + entry. getValue ( ) ) ;
}
Set< Map. Entry< String, Integer> > entries = m. entrySet ( ) ;
Iterator< Map. Entry< String, Integer> > iterator = entries. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
Map. Entry< String, Integer> next = iterator. next ( ) ;
System. out. println ( next. getKey ( ) + "---->" + next. getValue ( ) ) ;
}
Iterator< String> iterator1 = m. keySet ( ) . iterator ( ) ;
while ( iterator1. hasNext ( ) ) {
String key;
Integer value;
key= iterator1. next ( ) . toString ( ) ;
value= m. get ( key) ;
System. out. println ( key+ "--" + value) ;
}
}
}