泛型类、泛型方法、泛型接口

This commit is contained in:
2024-12-01 22:42:52 +08:00
parent e4d146365b
commit 26fcbfbd91
4 changed files with 143 additions and 2 deletions

View File

@@ -1,4 +1,29 @@
package chapter6;
import java.util.ArrayList;
import java.util.Iterator;
public class Example23 {
public static void main(String[] args) {
// 存放字符串的集合
ArrayList<String> names = new ArrayList<String>();
names.add("张三"); // √ 正确
Iterator it1 = names.iterator();
while (it1.hasNext()) {
String name = (String) it1.next();
System.out.println(name);
}
ArrayList students = new ArrayList();
names.add("张三");
Iterator it2 = students.iterator();
while (it2.hasNext()) {
String student = (String) it2.next();
System.out.println(student);
}
}
}