Collection 使用
package 集合类;
import java. util. ArrayList;
import java. util. Collection;
public class CollectionDemo2 {
public static void main ( String[ ] args) {
Collection collection = new ArrayList ( ) ;
Student s1= new Student ( "1" , 1 ) ;
Student s2= new Student ( "2" , 2 ) ;
Student s3= new Student ( "3" , 3 ) ;
collection. add ( s1) ;
collection. add ( s2) ;
collection. add ( s3) ;
System. out. println ( collection. toString ( ) ) ;
System. out. println ( collection) ;
}
}
package 集合类;
import java. util. ArrayList;
import java. util. Collection;
import java. util. Iterator;
public class CollectionDemo {
public static void main ( String[ ] args) {
Collection collection = new ArrayList ( ) ;
collection. add ( "西瓜" ) ;
collection. add ( "苹果" ) ;
collection. add ( "橘子" ) ;
System. out. println ( collection) ;
for ( Object object: collection) {
System. out. print ( object+ " " ) ;
}
System. out. println ( ) ;
Iterator it = collection. iterator ( ) ;
while ( it. hasNext ( ) ) {
String s = ( String) it. next ( ) ;
System. out. println ( s) ;
}
System. out. println ( collection. contains ( "西瓜" ) ) ;
System. out. println ( collection. isEmpty ( ) ) ;
}
}