-
package
v13; -
import
java.util.*; -
public
class Test { -
public
static void main( String [] args ){ -
List<</SPAN>String>
c = new ArrayList<</SPAN>String>();//采用泛型 使传入的对象类型只能是String -
c .add( "aaa" );
-
c .add( "bbb" );
-
c .add( "ccc" );
-
for
( int i = 0 ; i < c .size(); i ++){ -
String
s = c .get( i ); -
System. out .println( s );
-
System. out .println( c .get( i ));
-
}
-
-
Collection<</SPAN>String>
c2 = new HashSet<</SPAN>String>();//泛型使用 -
c2 .add( "aaa" );
-
c2 .add( "bbb" );
-
c2 .add( "ccc" );
-
for
(Iterator<</SPAN>String> it = c2.iterator();it.hasNext(); ){ -
String
s = it .next(); -
System. out .println( s );
-
}
-
Map<</SPAN>String,Integer>
m = new HashMap<</SPAN>String,Integer>();//泛型使用,注意参数 -
-
-
}
-
-
}
-
-
class
MyName implements Comparable{ -
int
age ; -
public
int compareTo(MyName my ){ -
if
( this . age < my . age ) return 1; -
else
if ( this . age > my . age ) return -1; -
else
return 0; -
}
-
}
Java 泛型 Generic
最新推荐文章于 2022-08-02 17:01:12 发布