StringBuffer构造方法
capacity()
length()
* * A:StringBuffer的构造方法:
* public StringBuffer():无参构造方法
* public StringBuffer(int capacity):指定容量的字符串缓冲区对象
* public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
* B:StringBuffer的方法:
* public int capacity():返回当前容量。 理论值(不掌握)
* public int length():返回长度(字符数)。 实际值(掌握)
* C:案例演示
* 构造方法和长度方法的使用
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.length()); //容器中的字符个数,实际值 0
System.out.println(sb.capacity()); //容器的初始容量,理论值 16
StringBuffer sb2 = new StringBuffer(10);
System.out.println(sb2.length()); //0
System.out.println(sb2.capacity()); //10
//
StringBuffer sb3 = new StringBuffer("lolpubg");
System.out.println(sb3.length()); //实际字符的个数 7
System.out.println(sb3.capacity()); //字符串的length + 初始容量 23
}
append(String str)
insert(int offset,String str)
/**
* * A:StringBuffer的添加功能 public StringBuffer append(String str):
* 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身 public StringBuffer insert(int offset,String
* str): 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
* StringBuffer是字符串缓冲区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符数组
* 当调用append()的方法时,不会再重新创建对象,会不断地向原缓冲区添加字符
* String 不可变
* StringBuffer可变
*/
public static void main(String[] args) {
// demo1();
StringBuffer sb = new StringBuffer("qwer");
sb.insert(3, "lolpubg"); //在指定位置添加元素,如果没有指定位置的索引就会报索引越界异常
System.out.println(sb);
String s = "ab";
s += "c";
}
private static void demo1() {
StringBuffer sb = new StringBuffer();
StringBuffer sb2 = sb.append(true);
// System.out.println(sb);//true
// System.out.println(sb2);
StringBuffer sb3 = sb.append("lolpubg");
StringBuffer sb4 = sb.append(100);
System.out.println(sb.toString()); // StringBuffer类中重写了toString方法,显示的是对象中的属性值
System.out.println(sb2.toString());
System.out.println(sb3.toString());
System.out.println(sb4.toString());
}
deleteCharAt(int index)
delete(int start,int end)
/**
* * A:StringBuffer的删除功能
* public StringBuffer deleteCharAt(int index):
* 删除指定位置的字符,并返回本身
* public StringBuffer delete(int start,int end):
* 删除从指定位置开始指定位置结束的内容,并返回本身
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
// sb.deleteCharAt(5); //缓冲区中这个索引上没有元素,
sb.append("lolpubg");
System.out.println(sb);
sb.deleteCharAt(4); //根据索引删除掉索引位置上对应的字符
System.out.println(sb);
sb.delete(0, 2); //删除的时候是包含头,不包含尾
System.out.println(sb);
sb.delete(0, sb.length()); //清空缓冲区
System.out.println(sb);
//
sb = new StringBuffer(); //不要用这种方式清空缓冲区,原来的会变成垃圾,浪费内存
System.out.println(sb);
}
replace(int start,int end,String str)
reverse()
/**
* * A:StringBuffer的替换功能
* public StringBuffer replace(int start,int end,String str):
* 从start开始到end用str替换
* [start,end):左包右不包
* B:StringBuffer的反转功能
* public StringBuffer reverse():
* 字符串反转
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("我爱总复习");
sb.replace(0, 3, "bai"); //替换
System.out.println(sb);
sb.reverse();
System.out.println(sb);
}
substring(int start)
substring(int start,int end)
/**
* * A:StringBuffer的截取功能
* public String substring(int start):
* 从指定位置截取到末尾(包含指定位置)
* public String substring(int start,int end):
* 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
* B:注意事项
* 注意:返回值类型不再是StringBuffer本身
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("woailolpubg");
String str = sb.substring(4);
System.out.println(str);
System.out.println(sb);
String str3 = sb.substring(4, 7);
System.out.println(str3);
}
String 和 StringBuffer转换
/**
* * A:String -- StringBuffer
* a:通过构造方法
* b:通过append()方法
* B:StringBuffer -- String
* a:通过构造方法
* b:通过toString()方法
* c:通过subString(0,length);
*/
public static void main(String[] args) {
// demo1();
demo2();
}
private static void demo2() {
StringBuffer sb = new StringBuffer("lolpubg");
String s1 = new String(sb); //通过构造将StringBuffer转换为String
System.out.println(s1);
String s2 = sb.toString(); //通过toString方法将StringBuffer转换为String
System.out.println(s2);
String s3 = sb.substring(0, sb.length()); //通过截取子字符串将StringBuffer转换为String
System.out.println(s3);
}
private static void demo1() {
StringBuffer sb1 = new StringBuffer("lolpubg"); //通过构造方法将字符串转换为StringBuffer对象 String -》 StringBuffer
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer();
sb2.append("lolpubg"); //通过append方法将字符串转换为StringBuffer对象 String -》 StringBuffer
System.out.println(sb2);
}
参数传递问题
/**
* * A:形式参数问题
* String作为参数传递
* StringBuffer作为参数传递
* B:案例演示
* String和StringBuffer分别作为参数传递问题
*
* 基本数据类型的值传递,只把值拷贝一份,把拷贝过后的值传过去
引用数据类型的值传递,把地址值传过去
String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的
*/
public static void main(String[] args) {
String s = "lolpubg";
System.out.println(s);
change(s);
System.out.println(s);
System.out.println("---------------------");
StringBuffer sb = new StringBuffer();
sb.append("lolpubg");
System.out.println(sb);
change(sb);
System.out.println(sb);
}
public static void change(StringBuffer sb) {
sb.append("全站三国");
}
public static void change(String s) {
s += "全站三国";
}
冒泡排序
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = { 5, 10, 3, 2, 7 };
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
Date类
/**
* * A:Date类的概述是util包下的,不能导入sql包的
* 类 Date 表示特定的瞬间,精确到毫秒。
* B:构造方法
* public Date()
* public Date(long date)
* C:成员方法
* public long getTime()
* public void setTime(long time)
*/
public static void main(String[] args) {
// demo1();
// demo2();
demo3();
}
public static void demo3() {
Date d1 = new Date();
// d1.setTime(1000); //设置毫秒值,改变时间对象
System.out.println(d1);
}
public static void demo2() {
Date d1 = new Date();
System.out.println(d1.getTime()); //通过时间对象获取毫秒值
System.out.println(System.currentTimeMillis()); //通过系统类的方法获取当前时间毫秒值
}
public static void demo1() {
Date d1 = new Date(); //如果没有传参数代表的是当前时间
System.out.println(d1);
Date d2 = new Date(0); //如果构造方法中参数传为0代表的是1970年1月1日
System.out.println(d2);
}
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//当前时间
Date d = new Date(10);
System.out.println(d);
//1970一月一日 早上8点0分0秒
Date d2 = new Date(1000);
System.out.println(d2);
}
SimpleDateFormat类
/**
* * A:DateFormat类的概述
* DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat
* B:SimpleDateFormat构造方法
* public SimpleDateFormat()
* public SimpleDateFormat(String pattern)
* C:成员方法
* public final String format(Date date)
* public Date parse(String source)
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// demo1();
// demo2();
// demo3();
demo4();
}
public static void demo4() throws ParseException {
//将时间字符串转换成日期对象
String str = "2000年08月08日 08:08:08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d = sdf.parse(str); //将时间字符串转换成日期对象
System.out.println(d);
}
public static void demo3() {
Date d = new Date(); //获取当前时间对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//创建日期格式化类对象
System.out.println(sdf.format(d)); //将日期对象转换为字符串
}
public static void demo2() {
Date d = new Date(); //获取当前时间对象
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat(); //创建日期格式化类对象
System.out.println(sdf.format(d)); //88-6-6 下午9:31
}
public static void demo1() {
// DateFormat df = new DateFormat(); //DateFormat是抽象类,不允许实例化
// DateFormat df1 = new SimpleDateFormat();
DateFormat df2 = DateFormat.getDateInstance(); //相当于父类引用指向子类对象,右边的方法返回一个子类对象
}
Calendar类
/**
* * A:Calendar类的概述
* Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
* B:成员方法
* public static Calendar getInstance()
* public int get(int field)
* C:成员方法
* public void add(int field,int amount)
* public final void set(int year,int month,int date)
* D:案例演示
* Calendar类的成员方法使用
*/
public static void main(String[] args) {
demo1();
// demo2();
}
public static void demo1() {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//System.out.println(c);
System.out.println(c.get(Calendar.YEAR)); //通过字段获取年
System.out.println(c.get(Calendar.MONTH)); //通过字段获取月,但是月是从0开始编号的
System.out.println(c.get(Calendar.DAY_OF_MONTH));//月中的第几天
System.out.println(c.get(Calendar.DAY_OF_WEEK));//周日是第一天,周六是最后一天
/*
* 星期天 1
* 星期一 2
* 星期二 3
* ...
* 星期五 6
* 星期六 7
*/
System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1))
+ "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));
//2019年05月31日星期五
}
public static void demo2() {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//c.add(Calendar.MONTH, -1); //对指定的字段进行向前减或向后加
//c.set(Calendar.YEAR, 2000); //修改指定字段
c.set(2000, 7, 8);
System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1))
+ "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
/*
* 将星期存储表中进行查表
* 1,返回值类型String
* 2,参数列表int week
*/
public static String getWeek(int week) {
String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week];
}
/*
* 如果是个数数字前面补0
* 1,返回值类型String类型
* 2,参数列表,int num
*/
public static String getNum(int num) {
/*if(num > 9) {
return "" + num;
}else {
return "0" + num;
}*/
return num > 9 ? "" + num : "0" + num;
}
小练习:计算自己现在活了多少天了
public static void main(String[] args) throws ParseException {
Date date = new Date();
String str = “1995年5月20日”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日”);
Date date2 = sdf.parse(str);
long days = (date.getTime()-date2.getTime())/1000/60/60/24;
System.out.println(days);
}