泛型
表示集合中 保存的数据的类型
泛型类
泛型类声明是 尖括号的字母 可以随便定义
泛型类的类型 在初始化这个类的对象 确定
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 <Q> void fun (Q q) {
System.out .println(q);
}
public void work () {
System.out .println(" 天天搬砖 一天搬400块 " );
}
}
泛型接口
interface InterA<Y>{
void fun (Y y);
}
实现类
class InterAImpl implements InterA <String >{
@Override
public void fun(String y) {
System.out.println(y);
}
}
/**
* 保存字符串
*/
public static void fun1 () {
ArrayList<String> list = new ArrayList<String>();
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 static void fun2 () {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("张三" ,15 ));
students.add(new Student("李四" ,16 ));
students.add(new Student("王五" ,17 ));
Iterator iterator = students.iterator();
Student student = (Student)iterator.next();
System.out.println(student.getName());
}
public static void fun3 () {
Worker<String> worker = new Worker<>();
worker.setW("haha" );
System.out .println(worker.getW());
worker.sayHi("hahaha" );
worker.print("???" );
Worker.fun("w" );
}
? extends E (向下限定)
? 是子类 继承 E是父类 ?号只能是E类的 子类
? super E (向上限定)
? 父类 E是子类 ?只能是E类的父类
public static void fun1 () {
ArrayList<Person> pList = new ArrayList<>();
pList.add(new Person("张三" , 13 ));
pList.add(new Person("李四" , 14 ));
ArrayList<Student> sList = new ArrayList<>();
sList.add(new Student("王五" , 16 ));
sList.add(new Student("赵六" , 17 ));
pList.addAll(sList);
System.out .println(pList);
}
int [] array = {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.out .println(asList2);
String[] array3 = {"wanglong" ,"wangsong" };
List<String> asList3 = Arrays.asList(array3);
System.out .println(asList3);
boolean isContions = asList3.contains("wanglong" );
System.out .println(asList3);
System.out .println(isContions);
// int ... num 相当于 传入的参数 是个数组
// int ... num 可以接受多个参数 只能是方法参数最后一个
// JDK 1.5 之后出来的
public static void fun2(int ... num) {
// 遍历
for (int i = 0 ; i < num.length; i++) {
System.out.println(num[i]);
}
}
集合中的删除方式
1. 不使用迭代器
public static void fun1() {
ArrayList<String> list = new ArrayList<>();
list .add("a" );
list .add("b" );
list .add("b" );
list .add("c" );
list .add("d" );
for (int i = 0 ; i < list .size(); i++) {
if (list .get(i).equals("b" )) {
list .remove(i--);
}
}
System.out.println(list );
}
2. 使用迭代器
public static void fun2() {
ArrayList< String > list = new ArrayList<> ();
list . add("a" );
list . add("b" );
list . add("b" );
list . add("c" );
list . add("d" );
Iterator< String > iterator = list . iterator();
while (iterator. hasNext()) {
String next = iterator. next();
if (next. equals ("b" )) {
iterator. remove();
}
}
System. out. println(list );
}
ArrayList<Student> list = new ArrayList<>();
list .add(new Student("c" , 19 ));
list .add(new Student("e" , 18 ));
list .add(new Student("a" , 13 ));
list .add(new Student("d" , 16 ));
list .add(new Student("b" , 17 ));
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 );
ArrayList<Student> list = new ArrayList<>();
list .add(new Student("c" , 19 ));
list .add(new Student("e" , 18 ));
list .add(new Student("a" , 13 ));
list .add(new Student("d" , 16 ));
list .add(new Student("b" , 17 ));
paiXu(list );
System.out.println(list );
}
public static void paiXu(ArrayList<Student> array ) {
for (int i = 0 ; i < array .size() - 1 ; i++) {
for (int j = 0 ; j < array .size() - 1 - i; j++) {
Student s1 = array .get(j);
Student s2 = array .get(j + 1 );
if (s1.getName().compareTo(s2.getName()) > 0 ) {
Collections.swap(array , j , j + 1 );
}
}
}
}
ArrayList<ArrayList<Student>> subject = new ArrayList<>();
ArrayList<Student> classes1 = new ArrayList<>();
classes1.add(new Student("张三" , 14 ));
classes1.add(new Student("李四" , 11 ));
ArrayList<Student> classes2 = new ArrayList<>();
classes2.add(new Student("赵六" , 12 ));
classes2.add(new Student("王五" , 13 ));
subject.add(classes1);
subject.add(classes2);
for (ArrayList<Student> list : subject) {
for (Student student : list ) {
System.out.println(student);
}
}
// 迭代器遍历
Iterator<ArrayList<Student>> arrayIterator = subject.iterator ()
while (arrayIterator.hasNext ()) {
ArrayList<Student> arrayList = arrayIterator.next ()
Iterator<Student> iterator = arrayList.iterator ()
while (iterator.hasNext ()) {
Student next = iterator.next ()
System.out .println (next)
}
}