泛型
作用:将数据类型作为参数级进行传递\
优点:使代码更加灵活方便
语法
<泛型名1,泛型名2...>
注意:可以定义多个泛型,每个之间使用逗号隔开
泛型名称自定义,一般使用K,T,V,Y
泛型的定义与使用
定义位置
方法
语法: 访问访问权限修饰符 修饰词 <K,T..> 返回值类型 方法名(形参列表){
方法体
}
使用位置:1.返回值类型
2.形参列表
3.方法体中变量类型
类
语法 访问权限修饰符 class 类名<K,T...>{
}
继承关系中,父类有泛型
语法
访问权限修饰符 class 子类名<K,T...>extends 父类名 <父泛型>{
}
注意:子类中必须包含所有父泛型
使用
1.创建对象
传递有泛型类的对象时,必须传入泛型对应的数据类型
2.继承
可以在当前类任何一个使用数据类型的地方使用
例:
package com.qf.demo03;
/**
* 泛型在类上定义的使用1
* 创建对象
*/
public class Demo03 {
public static void main(String[] args) {
Person<String, Integer, Character> p1 =
new Person<String, Integer, Character>
("张三", 4, '男');
Person<Integer, Integer, String> p2 =
new Person<Integer, Integer, String>(11, 18, "女");
String name = p1.getName();
Integer name2 = p2.getName();
}
}
package com.qf.demo04;
public class Demo04 {
public static void main(String[] args) {
}
}
interface A<K>{
}
interface B<V>{
}
class Test<K,V> implements A<K>,B<V>{
}
interface C<K,V> extends A<K>,B<V>{
}
集合
作用:存储一组数据类型相同的数据
与数组的区别
数组:长度固定的,不可变的,可以存储基本数据类型和引用数据类型
集合:长度不固定,可以改变,只能存储引用数据类型
体系
作用与区别
Collection提供的方法
package com.qf.demo05;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Demo05 {
public static void main(String[] args) {
ArrayList<String> list01 = new ArrayList<String>();
list01.add("10");
ArrayList<Integer> list02 = new ArrayList<Integer>();
}
}
package com.qf.demo07;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args) {
//Collection<Integer> col = new ArrayList<Integer>();
Collection<Integer> col = new HashSet<Integer>();
boolean b1 = col.add(10);
System.out.println(b1);
boolean b2 = col.add(10);
col.add(110);
System.out.println(b2);
System.out.println(col);
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(10);
boolean b3 = col.addAll(list);
System.out.println(col);
System.out.println(b3);
boolean b4 = col.remove(4);
System.out.println(col);
System.out.println(b4);
ArrayList<Integer> list2 = new ArrayList<Integer>();
// list2.add(1);
//list2.add(2);
list2.add(4);
boolean b5 = col.removeAll(list2);
System.out.println(col);
System.out.println(b5);
// col.clear();
System.out.println(col);
int size = col.size();
boolean b6 = col.contains(1);
System.out.println(b6);
boolean b7 = col.isEmpty();
System.out.println(b7);
System.out.println("--------foreach----------");
for(Integer n : col) {
System.out.println(n);
}
System.out.println("------使用迭代器-------");
//获取迭代器
Iterator<Integer> iterator = col.iterator();
//循环判断下一个是否有值
while(iterator.hasNext()) {
//有值,取值
Integer n = iterator.next();
System.out.println(n);
}
}
}
List提供的方法
package com.qf.demo07;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class TestList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(2);
System.out.println(list);
list.add(0, 10);
System.out.println(list);
HashSet<Integer> set = new HashSet<Integer>();
set.add(9);
set.add(8);
boolean b1 = list.addAll(1, set);
System.out.println(list);
System.out.println(b1);
System.out.println("---删除---");
Integer n = list.remove(0);
System.out.println(list);
System.out.println(n);
System.out.println("---截取---");
List<Integer> list2 = list.subList(2, list.size());
System.out.println(list2);
System.out.println(list);
System.out.println("---修改---");
list.set(0, -1);
System.out.println(list);
System.out.println("---查询---");
Integer x = list.get(0);
System.out.println(x);
System.out.println("查询指定元素所在的位置");
int index = list.indexOf(4);
System.out.println(index);
int lastIndexOf = list.lastIndexOf(10);
System.out.println(list);
System.out.println(lastIndexOf);
}
}