定义:
集合中保存数据的类型
创建泛型:
public static void fun() {
ArrayList< String > list = new ArrayList<> ();
list . add("a" );
list . add("b" );
list . add("c" );
list . add("d" );
Iterator< String > iterator = list . iterator();
while (iterator. hasNext()) {
String next = iterator. next();
System. out. println(next);
}
}
人类:
public class Person {
private String name;
private int age;
public Person () {
super ();
}
public Person (String name, int age) {
super ();
this .name = name;
this .age = age;
}
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this .age = age;
}
@Override
public String toString () {
return "姓名:" + name + " 年龄:" + age;
}
}
学生类:
public class Student extends Person {
public Student () {
super ();
}
public Student (String name, int age) {
super (name, age);
}
}
泛型编译期报错:
public static void fun () {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("鹏鹏" , 17 ));
students.add(new Student("水水" , 17 ));
students.add(new Student("茜茜" , 17 ));
Iterator<Student> iterator = students.iterator();
Student student = iterator.next();
System.out .println(student);
}
泛型类:
public class Worker<W> {
private W w;
public W getW () {
return this .w;
}
public void setW (W w) {
this .w = w;
}
public void sayHi (W w) {
System.out .println(w);
}
public <Z> void print (Z z) {
System.out .println(z);
}
public static <S> void fun (S s) {
System.out .println(s);
}
public void work () {
System.out .println("天天搬砖 一天400块" );
}
}
public static void fun () {
Worker<String> worker = new Worker<>();
worker.setW("哈哈" );
System.out .println(worker.getW());
worker.sayHi("哈哈哈" );
worker.print("哈哈哈哈" );
Worker.fun("哈哈哈哈哈" );
}
泛型接口:
interface InterA <A > {
public abstract void fun(A a);
}
class InterAImpl implements InterA <String >{
@Override
public void fun(String a) {
System.out.println(a);
}
}
? extends E(向下限定):
? 是子类 继承 E 是父类 问号只能是E 类的子类
public static void fun1 () {
ArrayList<Person> pList = new ArrayList<>();
pList.add(new Person("张三" , 11 ));
pList.add(new Person("李四" , 11 ));
ArrayList<Student> sList = new ArrayList<>();
sList.add(new Student("王五" , 14 ));
sList.add(new Student("赵六" , 15 ));
pList.addAll(sList);
System.out .println(pList);
}
? super E(向上限定):
? 是父类 E 是子类 问号只能是E 类的父类
// int ... num 相当于 传入的参数是个数组
// int ... num 可以接受多个参数 只能是方法参数的最后一个
// JDK1.5出来的
public static void fun(int j, int ... num) {
// 遍历
for (int i = 0 ; i < num.length; i++) {
System.out.println(num[i]);
}
System.out.println("J是:" + j);
}
举例:
public static void fun () {
int [] array = new int []{1 ,2 ,3 ,4 ,5 };
List<int []> asList = Arrays.asList(array);
System.out .println(asList);
Integer[] array2 = {1 ,2 ,3 ,4 ,5 };
List<Integer> asList2 = Arrays.asList(array2);
System.err.println(asList2);
String[] array3 = {"wonglong" , "wangsong" };
List<String> asList3 = Arrays.asList(array3);
System.out .println(array3);
boolean contains = asList3.contains("wanglong" );
System.out .println(asList3);
System.out .println(contains);
}
集合中删除:
不用迭代器:
public static void fun() {
ArrayList<String> list = new ArrayList<>();
list .add("a" );
list .add("b" );
list .add("c" );
list .add("b" );
list .add("d" );
list .add("e" );
for (int i = 0 ; i < list .size(); i++) {
if (list .get(i).equals("b" )) {
list .remove(i--);
}
}
System.out.println(list );
}
使用迭代器:
public static void fun() {
ArrayList< String > list = new ArrayList<> ();
list . add("a" );
list . add("b" );
list . add("c" );
list . add("b" );
list . add("d" );
list . add("e" );
Iterator< String > iterator = list . iterator();
while (iterator. hasNext()) {
String string = iterator. next();
if (string . equals ("b" )) {
iterator. remove();
}
}
System. out. println(list );
}
foreach:
public static void fun() {
ArrayList< String > list = new ArrayList<> ();
list . add("a" );
list . add("b" );
list . add("c" );
list . add("b" );
list . add("d" );
list . add("e" );
for (String string : list ) {
System. out. println(string );
}
}
实例应用:
public static void fun() {
ArrayList<Student> list = new ArrayList<>();
list .add(new Student("张三" , 19 ));
list .add(new Student("李四" , 25 ));
list .add(new Student("王五" , 17 ));
list .add(new Student("赵六" , 23 ));
list .add(new Student("孙七" , 18 ));
for (int i = 0 ; i < list .size() - 1 ; i++) {
for (int j = 0 ; j < list .size() - 1 - i; j++) {
Student s1 = list .get(j);
Student s2 = list .get(j + 1 );
if (s1.getAge() > s2.getAge()) {
list .set (j + 1 , s1);
list .set (j, s2);
}
}
}
System.out.println(list );
}
public static void fun() {
ArrayList<Student> list = new ArrayList<>();
list .add(new Student("张三" , 19 ));
list .add(new Student("李四" , 25 ));
list .add(new Student("王五" , 17 ));
list .add(new Student("赵六" , 23 ));
list .add(new Student("孙七" , 18 ));
for (int i = 0 ; i < list .size() - 1 ; i++) {
for (int j = 0 ; j < list .size() - 1 - i; j++) {
Student s1 = list .get(j);
Student s2 = list .get(j + 1 );
if (s1.getName().compareTo(s2.getName()) > 0 ) {
Collections.swap(list , j, j + 1 );
}
}
}
System.out.println(list );
}
public static void fun () {
/*
* 创建一个集合保存 java学科(集合)
* 学科中有两个班(集合) 班里有学生(使用泛型)
* 遍历java学科中的学生
*/
ArrayList<ArrayList<Student>> subject = new ArrayList<>();
ArrayList<Student> class1 = new ArrayList<>();
class1.add(new Student("张三" , 15 ));
class1.add(new Student("李四" , 16 ));
ArrayList<Student> class2 = new ArrayList<>();
class2.add(new Student("王五" , 18 ));
class2.add(new Student("赵六" , 16 ));
subject.add(class1);
subject.add(class2);
/*
for (ArrayList<Student> list : subject) {
for (Student student : list) {
System.out.println(student);
}
}
*/
Iterator<ArrayList<Student>> sub = subject.iterator();
while (sub.hasNext()) {
ArrayList<Student> aList = sub.next();
Iterator<Student> classes = aList.iterator();
while (classes.hasNext()) {
Student student = classes.next();
System.out.println(student);
}
}
}