泛型
泛型是指在类定义时不指定类中信息的具体数据类型,而是用一个标识符来代替,当外部实例化对象时来指定具体的数据类型。
有了泛型,我们就可以在定义类或接口的时候不指定类中信息的具体数据类型,而是在实例化对象的时候再指定具体的数据类型,极大地提升类的扩展性,一个类可以装载各种不同的数据类型,泛型可以指代类中成员变量的具体数据类型,方法的返回值类型以及方法的参数数据类型。
没有泛型就存在数据不安全的问题
使用泛型就可以避免数据不安全的隐患
指定泛型就是统一集合中的数据类型
泛型是可以选择是否要定义,如果不定义,则相当于 Object,如果定义,则根据定义的数据类型来选择
public class Time<E> {
private E value;
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
Time<Integer> time = new Time<>();
time.setValue(10);
System.out.println("现在的时间是:" + time.getValue());
Time<String> time1 = new Time<>();
time1.setValue("十点整");
System.out.println("现在的时间是:" + time1.getValue());
}
}
泛型和 Object 的区别
Object 是 JDK 提供的一个类
泛型是开发者自定义的一个标识符,没有具体的值,实例化对象的时候再去指定具体的数据类型
如果实例化的时候不指定具体的数据类型,则此时泛型自动编程 Object
指定 Integer 泛型就成为 Integer
指定 String 泛型就成为 String
不指定 泛型就成为 Object
泛型在实例化的时候必须要找到一个具体的数据类型
public class Time<H,M,S> {
private H hour;
private M minute;
private S second;
public H getHour() {
return hour;
}
public void setHour(H hour) {
this.hour = hour;
}
public M getMinute() {
return minute;
}
public void setMinute(M minute) {
this.minute = minute;
}
public S getSecond() {
return second;
}
public void setSecond(S second) {
this.second = second;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
Time<String,Integer,Float> time = new Time();
time.setHour("十点");
time.setMinute(10);
time.setSecond(10.0f);
System.out.println("现在的时间是:" + time.getHour() + ":" + time.getMinute() + ":" + time.getSecond());
}
}
枚举
枚举是一种有特定取值区间的数据类型,本质上就是一种类,具有简洁、安全、方便等特点
枚举的值被约束到一个特定的范围内,只能从这个范围内取值
public class Week {
public static final int MONDAY=1;
public static final int TUESDAY=2;
public static final int WEDNESDAY=3;
public static final int THURSDAY=4;
public static final int FRIDAY=5;
public static final int SATURDAY=6;
public static final int SUNDAY=7;
private Week(){
}
}
public enum WeekEnum {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
}
import java.util.*;
public class Test {
public static void main(String[] args) {
System.out.println(WeekEnum.FRIDAY);
System.out.println(Week.FRIDAY);
}
}
StringBuffer
StringBuffer 是对 String 的一种优化和升级,实际开发中使用 String 会存在一个问题,一旦 String 创建,值是无法修改的,无法在原地址上进行修改,必须重新创建一个新的对象,重新开辟一块新的内存空间来存储新的值
String 底层是数组,数组一旦创建,长度是无法修改的
为了解决 String 值无法修改的问题,创建了 StringBuffer 这个类来解决这一问题
String 底层数组的长度是由值来决定的
StringBuffer 底层的数组长度默认为 16
无论用 StringBuffer 创建什么样的字符串,默认都会预留 16 个位置来进行修改
如果修改的值长度大于 16,则 StringBuffer 会自动进行数组扩容,16 - 32 - 64
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
方法 | 描述 |
---|---|
public StringBuffer() | 无参构造,创建一个空的StringBuffer |
public StringBuffer(String str) | 有参构造 |
public synchronized int length() | 返回 StringBuffer 的长度 |
public synchronized char charAt(int index) | 返回指定位置的字符 |
public synchronized StringBuffer append(String str) | 给StringBuffer追加字符串 |
public synchronized StringBuffer delete(int start,int end) | 删除指定区间内的字符 |
public synchronized StringBuffer deleteCharAt(int index) | 删除指定位置的字符 |
public synchronized StringBuffer replace(int start,int end,String str) | 将指定区间的值替换为 str |
public synchronized String substring(int start) | 截取字符串从指定位置到结尾 |
public synchronized String substring(int start,int end) | 截取字符串从指定位置开始到指定位置结束 |
public synchronized StringBuffer insert(int offset,String str) | 向指定位置插入 str |
public int indexOf(String str) | 从头开始查找指定字符的位置 |
public int indexOf(String str,int index) | 从指定位置开始查找字符的位置 |
public synchronized StringBuffer reverse() | 进行反转 |
public synchronized String toString() | 返回StringBuffer对应的String |
import java.util.*;
public class Test {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
System.out.println(stringBuffer);
System.out.println(stringBuffer.length());
stringBuffer = new StringBuffer("Hello World");
System.out.println(stringBuffer);
System.out.println(stringBuffer.charAt(2));
stringBuffer = stringBuffer.append("Java");
System.out.println(stringBuffer);
stringBuffer = stringBuffer.delete(3, 6);
System.out.println(stringBuffer);
stringBuffer = stringBuffer.deleteCharAt(3);
System.out.println(stringBuffer);
stringBuffer = stringBuffer.replace(2, 3, "StringBuffer");
System.out.println(stringBuffer);
String substring = stringBuffer.substring(2);
System.out.println(substring);
substring = stringBuffer.substring(2, 8);
System.out.println(substring);
stringBuffer = stringBuffer.insert(6, "six");
System.out.println(stringBuffer);
System.out.println(stringBuffer.indexOf("e"));
System.out.println(stringBuffer.indexOf("e", 6));
stringBuffer = stringBuffer.reverse();
System.out.println(stringBuffer);
String string = stringBuffer.toString();
System.out.println(string);
}
}
日期类
Date 和 Calendar
Date 的使用很简单,直接创建对象,表示当前的系统时间
格式化
标记 | 描述 |
---|---|
y | 年 |
M | 月 |
m | 分钟 |
d | 天 |
H | 小时,24小时制 |
h | 小时,12小时制 |
s | 秒 |
import java.text.SimpleDateFormat;
import java.util.*;
public class Test {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(date);
System.out.println(format);
}
}
Date 只能表示当前的系统时间,使用比较简单,功能也有限,复杂的日期业务需要使用 Calendar,可以对日期进行操作。
常量 | 描述 |
---|---|
YEAR | 年 |
MONTH | 月 |
DAY_OF_MONTH | 天 |
DAY_OF_YEAR | 一年中的第几天 |
HOUR_OF_DAY | 小时 |
MINUTE | 分钟 |
SECOND | 秒 |
MILLISECOND | 毫秒 |
方法 | 描述 |
---|---|
getInstance() | 获取系统对应的 Calendar 对象 |
set(int field,int value) | 给静态常量赋值 |
get(int field) | 取出静态常量 |
Date getTime() | 获取 Calendar 对应的 Date 对象 |
import java.text.SimpleDateFormat;
import java.util.*;
public class Test {
public static void main(String[] args) {
//计算2023-8-6所在的周是2023年的第几周
// Calendar calendar = Calendar.getInstance();
// calendar.set(Calendar.YEAR, 2023);
// calendar.set(Calendar.MONTH, 7);
// calendar.set(Calendar.DAY_OF_MONTH, 6);
// int i = calendar.get(Calendar.WEEK_OF_MONTH);
// System.out.println(i);
//2023-8-6往前推1000天的日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2023);
calendar.set(Calendar.MONTH, 7);
calendar.set(Calendar.DAY_OF_MONTH, 6);
int i = calendar.get(Calendar.DAY_OF_YEAR);
i = i - 1000;
calendar.set(Calendar.DAY_OF_YEAR, i);
Date time = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(time);
System.out.println(format);
}
}
Calendar 的使用
1、需要进行输入,将被计算的日期输入到 Calendar 对象中
通过 set 进行输入
根据不同的静态常量,赋不同的值
2、进行运算
3、取值,根据静态常量进行取值
Java 的实用类,JDK 提供的一部分工具类,可以帮助我们进行一些特定业务的运算