今天学习了泛型,collections类、枚举、包装类
泛型
将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性,本质是参数化类型
泛型集合可以约束集合内的元素类型
典型泛型集合ArrayList<E>、HashMap<K,V>
<E>、<K,V>表示该泛型集合中的元素类型
泛型集合中的数据不再转换为Object
泛型修改昨天的代码
public static void main(String[] args) {
NewsTitle nt1 = new NewsTitle(1001,"佩罗西死了","张氏新闻");
NewsTitle nt2 = new NewsTitle(1002,"中国成功发射一箭十六星","中国航天");
NewsTitle nt3 = new NewsTitle(1003,"男子救落水5人后遇难同伴讲述经过","民间观察");
NewsTitle nt4 = new NewsTitle(1004,"苏氏祖祠。苏炳添是苏东坡第29代孙","生活观察");
NewsTitle nt5 = new NewsTitle(1005,"家长花200万给娃集奥特曼卡没集齐","四川观察");
//准备容器:创建集合对象,创建ArrayList对象
ArrayList<NewsTitle> al = new ArrayList<NewsTitle>();
//将数据存储到集合中,向集合中添加元素不是通过元素下标来赋值,而是通过集合对象调用方法实现
al.add(nt1);
al.add(nt2);
al.add(nt3);
al.add(nt4);
al.add(nt2);
//可以通过集合对象调用方法实现对集合中元素的操作
//获取集合中的元素个数
int size =al.size();
System.out.println(size);
//获取集合中指定位置的元素
// Object obj1 = al.get(0);
// NewsTitle tit=(NewsTitle)obj1;
// System.out.println(tit);
for (int i = 0; i <al.size(); i++) {
NewsTitle tit = al.get(i);
System.out.println(tit);
}
System.out.println("---------");
for (NewsTitle ntw : al) {
System.out.println(ntw);
}
System.out.println("--------------------");
//使用迭代器遍历集合
/*
* 集合对象调用iterator()方法将集合中的所有元素按照顺序取出来放入到迭代器容器里面
* 然后通过迭代器对象调用hasnext()方法判断迭代器中是否有元素,有元素就通过next()方法取出这个元素
* 判断一个就取出一个,判断一个就取出一个,所以可以使用循环来取出里面所有的元素
*/
Iterator<NewsTitle> it=al.iterator();
while(it.hasNext()){
NewsTitle tit=it.next();
System.out.println(tit);
}
System.out.println("---------------------");
//将数据存贮到集合中的指定位置
al.add(1,nt5);
Iterator<NewsTitle> it1=al.iterator();
while(it1.hasNext()){
NewsTitle tit=it1.next();
System.out.println(tit);
}
//删除集合中的元素
System.out.println("------------");
Object ob=al.remove(1);
System.out.println(ob);
al.remove(nt3);
for (int i = 0; i <al.size(); i++) {
NewsTitle tit = al.get(i);
System.out.println(tit);
}
System.out.println("---------");
//判断集合中是否含有指定元素
boolean result = al.contains(nt5);
System.out.println(result);
//判断集合是不是空的
System.out.println("集合是空的:"+al.isEmpty());
//将集合转换成数组
System.out.println("*************");
Object[] objects=al.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
System.out.println("*************");
//清空集合
al.clear();
System.out.println("集合是空的:"+al.isEmpty());
System.out.println(al.size());
}
泛型的好处
类型安全
消除强制类型转换
潜在的性能收益
Collections类
Java集合框架将针对不同数据结构算法的实现都保存在工具类中
Collections和Collection不同,前者是集合的操作类,后者是集合接口
Collections类常用方法
Collections提供的常用静态方法
sort():排序
ArrayList<String> al = new ArrayList<String>();
al.add("qwe");
al.add("abf");
al.add("abc");
al.add("wer");
al.add("hgd");
al.add("acb");
al.add("ahj");
al.add("lok");
for (String string : al) {
System.out.println(string);
}
//集合元素升序排序
System.out.println("--------");
System.out.println("排序后");
Collections.sort(al);
for (String string : al) {
System.out.println(string);
}
binarySearch():查找
//查找元素,注意:查找之前要升序排序,否则不能保证结果的正确性
int index=Collections.binarySearch(al, "acb");
System.out.println(index);
max()\min():查找最大\最小值
System.out.println(Collections.max(al));
System.out.println(Collections.min(al));
枚举
再给性别赋值的时候,正常情况下只能赋予男或女,其他值都是非法的
所以有两个方法
方法一:条件判断 方法二:使用枚举
方法一:
public String name;
public String gender;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student stu = new Student();
stu.name="张三";
stu.gender="你好";
//再给性别赋值的时候,正常情况下只能赋予男或女,其他值都是非法的
while(!(stu.gender.equals("男")||stu.gender.equals("女"))){
System.out.println("性别只能为男或者女");
stu.gender = sc.next();
}
System.out.println(stu.gender);
sc.close();
}
方法二:使用枚举 枚举类是由一组固定的常量组成的类型
public enum Genders {
//枚举类是由一组固定的常量组成的类型
男,女;
}
public class Student {
public String name;
public Genders gender;
public static void main(String[] args){
Student stu = new Student();
stu.name="张三";
stu.gender=Genders.男;
}
}
枚举的优点
类型安全
易于输入
代码清晰
包装类
包装类把基本类型数据转换为对象 每个基本类型在java.lang包中都有一个相应的包装类
包装类的作用
提供了一系列实用的方法
集合不允许存放基本数据类型数据,存放数字时,要用包装类型
包装类的构造方法
所有包装类都可以将与之对应的基本数据类型作为参数,来构造他们的实例
除Character类外,其他包装类可将一个字符串作为参数构造它们的实例Number类型包装类使用字符串作为参数构造实力的时候,字符串需要能够转化成数字的字符串,否则就会报错(NumberFormatException)数据格式化异常
Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
byte num1=100;
Byte byte1 = new Byte(num1);
//输出方式和输出结果没有区别
System.out.println(num1);
System.out.println(byte1);
short num2=100;
Short short1 = new Short(num2);
int num3=100;
Integer int1= new Integer(num3);
boolean num4 = true;
Boolean bool1 = new Boolean(num4);
System.out.println("---------------");
Byte byte2 = new Byte("123");
byte b1 = new Byte(byte2);
System.out.println(b1);//字符串123
System.out.println(byte2);//数字123
Boolean boolean2 = new Boolean("qwe");
System.out.println(boolean2);
public static void main(String[] args) {
//输出byte类型的最大值和最小值
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}