1:Java之冒泡排序
package com.itcast.afu01;
import java.util.Arrays;
/**
*冒泡排序:
* 一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将比较大的数据放在后面,
* 一次对所有数据进行操作,直到所有数据按要求完成排序.
*
* 如果有n个数据进行排序,总共需要比较 n-1 次
* 每次比较完毕, 下一次比较就会少一个数据参与
*/
public class TestDemo01 {
public static void main(String[] args) {
int[] arr = {45,25,13,99,88,66};
System.out.println("排序前:" + Arrays.toString(arr)); // 排序前:[45, 25, 13, 99, 88, 66]
//外循环控制比较轮数 这里减1,是控制每轮比较的次数
for (int i = 0; i < arr.length - 1; i++) {
//内循环控制比较次数 -1是为了避免索引越界,-x是为了调高比较效率 不- 1 的话 arr[x+ 1] 这里会出现数组越界.
for (int x = 0; x < arr.length - 1 - i; x++) {
//判断做比较 如果前一个元素比后边的元素大, 就叫唤它们的位置.
if (arr[x] > arr[x+ 1]){
int temp = arr[x];
arr[x] = arr[x+ 1];
arr[x+ i] = temp;
}
}
}
System.out.println("排序后的数组为:" + Arrays.toString(arr));
// 循环结束 打印排序后的数组
}
}
2: 包装类:
基本类型包装类(记忆)
-
基本类型包装类的作用
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
-
基本类型对应的包装类
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
Integer类(应用)
-
Integer类概述
包装一个对象中的原始类型 int 的值
-
Integer类构造方法
方法名 说明 public Integer(int value) 根据 int 值创建 Integer 对象(过时) public Integer(String s) 根据 String 值创建 Integer 对象(过时) public static Integer valueOf(int i) 返回表示指定的 int 值的 Integer 实例 public static Integer valueOf(String s) 返回一个保存指定值的 Integer 对象 String
/**
* int和String类型的相互转换(记忆)
*/
public class TestDemo03 {
public static void main(String[] args) {
// int -> String
int num = 18000;
//方式1 通过字符串拼接的方式
String s1= "" + num;
System.out.println(s1); // 18000
//方式2 public static String valueOf(int i)返回int参数的字符串表示形式。 静态的 直接通过类. 的方式访问
String s2 = String.valueOf(num);
System.out.println(s2); // 18000
System.out.println("----------------------------");
//String -> int
String s = "15000";
//方式1 String -> Integer -> int
Integer i = Integer.valueOf(s);
// public int intValue()
int i1 = i.intValue();
System.out.println(i1); //15000
//方式2 public static int parseInt(String s)
int i2 = Integer.parseInt(s);
System.out.println(i2); //15000
}
}
3: Date类
Date类常用方法(应用)
-
常用方法
方法名 说明 public long getTime() 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 public void setTime(long time) 设置时间,给的是毫秒值
public class TestDemo04 {
public static void main(String[] args) {
//创建日期对象 public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
Date d1= new Date();
System.out.println(d1); // 打印出系统当前时间
//public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
long date = 1000*60*60; //1h
Date d2 = new Date(date);
System.out.println(d2); //
}
}
Date类常用方法(应用)
-
常用方法
方法名 说明 public long getTime() 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 public void setTime(long time) 设置时间,给的是毫秒值
4:SimpleDateFormat类(应用)
-
SimpleDateFormat类概述
SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
我们重点学习日期格式化和解析
-
SimpleDateFormat类构造方法
方法名 说明 public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式 public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 -
SimpleDateFormat类的常用方法
- 格式化(从Date到String)
- public final String format(Date date):将日期格式化成日期/时间字符串
- 解析(从String到Date)
- public Date parse(String source):从给定字符串的开始解析文本以生成日期
- 格式化(从Date到String)
public class TestDemo04 {
public static void main(String[] args) throws ParseException {
//格式化 Date - > String
Date d1 = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat(); 无参数构造方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s1 = sdf.format(d1);
System.out.println(s1); //20-11-5 下午6:10 2020年11月05日 18:12:25
System.out.println("-----------------------");
//从String 到 date
String s2= "2020年10月15日 17:5:55";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //有参构造
Date p1 = sdf1.parse(s2); //调用方法
System.out.println(p1); //Thu Oct 15 17:05:55 CST 2020
}
}
案例需求
定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法
/**
* 工具类 :
* 构造方法私有
* 成员方法静态
*/
public class DateUtils {
private DateUtils(){
}
/*
把日期转为指定格式的字符串
返回值类型:String
参数:Date date, String format
*/
public static String toStringDate(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String f = sdf.format(date);
return f;
}
/*
把字符串解析为指定格式的日期
返回值类型:Date
参数:String s, String format
*/
public static Date stringToDate(String s,String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date p = sdf.parse(s);
return p;
}
}
public class TestDemoUtils {
public static void main(String[] args) throws ParseException {
Date d1 = new Date();
String s1 = DateUtils.toStringDate(d1, "yyyy年MM月dd日 HH:mm:ss");
System.out.println(s1); //2020年11月05日 18:56:38
String s2 = DateUtils.toStringDate(d1, "yyyy年MM月dd日");
System.out.println(s2); //2020年11月05日
System.out.println("-------------------------");
String s ="2020年10月15日";
Date d = DateUtils.stringToDate(s, "yyyy年MM月dd日");
System.out.println(d); //Thu Oct 15 00:00:00 CST 2020
}
}
5: Calendar类(应用)
-
Calendar类概述 抽象类
public abstract class Calendar extends Object
implements Serializable, Cloneable, Comparable Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。
该方法返回一个Calendar 对象。
其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();
-
Calendar类常用方法
方法名 说明 public int get(int field) 返回给定日历字段的值 public abstract void add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段 public final void set(int year,int month,int date) 设置当前日历的年月日
public class TestDemo05 {
public static void main(String[] args) {
//获取日历类对象
Calendar c = Calendar.getInstance();
//public int get(int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1; //默认月份从零开始 所以要 +1
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日"); //当前年月日 2020年11月5日
//public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
//需求1:4年前的今天
c.add(Calendar.YEAR,-4);
int year1 = c.get(Calendar.YEAR);
int month1= c.get(Calendar.MONTH) + 1; //默认月份从零开始 所以要 +1
int date1 = c.get(Calendar.DATE);
System.out.println(year1 + "年" + month1 + "月" + date1 + "日"); //2016年11月5日
//需求2:10年后的100天前
c.add(Calendar.YEAR,10);
c.add(Calendar.DATE,-100);
int year2 = c.get(Calendar.YEAR);
int month2= c.get(Calendar.MONTH) + 1; //默认月份从零开始 所以要 +1
int date2 = c.get(Calendar.DATE);
System.out.println(year2 + "年" + month2 + "月" + date2 + "日"); //2030年7月28日
//public final void set(int year,int month,int date):设置当前日历的年月日
c.set(1997,10,15);
int year3= c.get(Calendar.YEAR);
int month3= c.get(Calendar.MONTH) ;
int date3= c.get(Calendar.DATE);
System.out.println(year3 + "年" + month3 + "月" + date3 + "日"); //1997年10月15日
}
}