带妹学Java第八天(至Calendar)

重点

1.冒泡排序

原理:相临的两个数比较,大的数放在右边

 int[] arr = {18,30,69,29,25,10,0};
		
		//1.比较4轮
		for(int i=0;i<arr.length-1;i++){
			System.out.println("第" + (i + 1) + "轮比较");
		
			//2.打印出每一轮比较的对应索引
			for(int j=0;j < arr.length - 1 - i; j++){
				//System.out.println(j);
				int left = j;
				int right = j + 1;
				System.out.println(left + "-" + right);
				
				//换位置
				if(arr[left] > arr[right]){
					int tmp = arr[left];
					arr[left] = arr[right];
					arr[right] = tmp;
				}
			}
		
		}

图示:
冒泡排序

2.数组的高级选择排序

原理:第一个数依次跟后面的数比较,小的放前面

	int[] arr = {24,69,80,57,13};
		
		//比较的轮数
		for(int i=0;i<arr.length - 1;i++){
			System.out.println("第" + (i+1) + "轮比较");
			
			for(int j = i + 1 ; j < arr.length;j++ ){
				System.out.println(i + "=" + j);
				
				//对换位置
				if(arr[i] > arr[j]){
					int tmp = arr[i];
					arr[i] = arr[j];
					arr[j] = tmp;
				}
			}
		}

图示:
选择排序

3.数组的二分查找法(拆半查找)

核心思想:取中间的索引来进行查找
优点:平均查找性能比较好
缺点:查找的数组必须是有顺序

int[] arr = {11,22,33,44,55,66,77};
		
		int num = 66;
		
		int min = 0;
		int max =  arr.length - 1;
		int mid = (min + max) / 2; 
		System.out.println("min:" + min + " mid:" + mid + " max:" + max);
		
		//查找的算法
		int index = -1;//如果算到最后,index的值没有改变,代表num在arr中不存在
		
		while(true){
			
			if(arr[mid] == num){//打到查找的数
				index = mid;
				break;
			}
			
			if(arr[mid] > num){//num在左边
				max = mid - 1;
				mid = (min + max) / 2;
			}
			
			if(arr[mid] < num){//num 在右边
				min = mid + 1;
				mid = (min + max) / 2;
			}
			
			if(min > max){//不符合逻辑
				break;//退出循环
			}
			
		}
		
		System.out.println(num + " 索引:" + index);

图示:
二分查找

4.Arrays工具类

static String toString(int[] arr) 把数组转成字符串 [1, 3, 4]
static void sort(int[] arr) 排序方法
static index binarySearch(int[] arr,int key) 查找数字在数组中的位置

5.基本数据类型的包装类

  • 包装类主要作用:提供数据类型转换,int -> String 或者String -> int
    每一种基本的数据类型都有对应的包装类

int -> Integer
byte -> Byte
short -> Short
long -> Long
double -> Double
float -> Float
boolean -> Boolean
char -> Character

6.String和int转换的方式

int 转 String
1.和""进行拼接
2.public static String valueOf(int i) (String类方法)
3.int – Integer – String(Integer类的toString方法())
4.public static String toString(int i)(Integer类的方法)

String 转 int
1.String – Integer – int
2.public static int parseInt(String s)

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String和int类型的相互转换
//		一、int 转 String
		int a = 120;
//		1.1和""进行拼接
		String s1 = a + "";
		
//		1.2public static String valueOf(int i) (String类方法)
		String s2 = String.valueOf(a);
		
//		1.3int -- Integer -- String(Integer类的toString方法())
		Integer integer = new Integer(a);
		String s3 = integer.toString();
		
//		1.4public static String toString(int i)(Integer类的方法)
		String s4 = Integer.toString(a);
		
		System.out.println("s1:" + s1);
		System.out.println("s2:" + s2);
		System.out.println("s3:" + s3);
		System.out.println("s4:" + s4);
		
//		二、String 转 int
		String str = "520";
//		String -- Integer – int
		Integer int1 = new Integer(str);
		int b = int1.intValue();
		
//		public static int parseInt(String s) [常用]
		int c = Integer.parseInt(str);
		
		System.out.println("b:" + b);
		System.out.println("c:" + c);
	}

7.自动装箱和拆箱

  • 自动装箱:把基本数据类开自动转成包装类
    Integer i = 10;

  • 自动拆箱:把包装类型转成基本数据类型
    Integer i = new Integer(10);
    int a = i;

8.Math的方法列举

// 1.public static int abs(int a) 取绝对值
// 2.public static double ceil(double a) //向上(往大数)取整
// 3.public static double floor(double a) // 向下(往小数)取整
// 4.public static int max(int a,int b) 取最大值, min方法自学
// 5.public static double pow(double a,double b) a^b a的b次方
// 6.public static double random() 生成0~0.99999的随机数
// 7.public static int round(float a) 四舍五入取整数。
// 8.public static double sqrt(double a) 值的正平方根

9.Random类

》Random类的概述
此类用于产生随机数
如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
》构造方法
public Random()
public Random(long seed)
》成员方法
public int nextInt()
public int nextInt(int bound)(重点掌握)

public class Demo01 {

	public static void main(String[] args) {
			
		//test1();
		
		/*如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,
		它们将生成并返回相同的数字序列*/
		Random r1 = new Random(10000);
		Random r2 = new Random(10000);
		
		System.out.println(r1.nextInt());
		System.out.println(r2.nextInt());
		
		System.out.println("==============");
		System.out.println(r1.nextInt());
		System.out.println(r2.nextInt());
		
		System.out.println("==============");
		System.out.println(r1.nextInt());
		System.out.println(r2.nextInt());
	}

	public static void test1() {
		//1.创建Random实例
		Random r1 = new Random();
		
		/*for(int i=0;i<10;i++){//产生10个int随机数
			System.out.println(r1.nextInt());
		}*/
		
	/*	the next pseudorandom, uniformly distributed int value between zero (inclusive) 
		and bound (exclusive) from this random number generator's sequence
*/		for(int i=0;i<10;i++){
			//生成0~9,不包括10
			System.out.println(r1.nextInt(10));
		}
	}

}

10.System类

4个方法
public static void gc() 运行垃圾回收
public static void exit(int status) 退出程序
public static long currentTimeMillis() 从1970年1月1日到现在走的毫秒数
pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 数组拷贝

11.BigInteger类

当运算时,超过最大的int值时,可以用BigIntger来做加减乘除运算
BigInteger的方法
*add 加法
*substract 减法
*multiply 乘法
*divide 除方法
*divideAndRemainder 除和余数

12.BigDecimal类

用于计算小数点,因为floal/double在运算时,可能会出现误差,使用BigDecimal可以减少误差
BigDecimal
*add 加法
*substract 减法
*multiply 乘法
*divide 除方法

  • 记住:
  • 以后在公司做关于金钱结算模块的时候,尽量用BigDecimal这个类
  • 因为这个类可以减少误差
import java.math.BigDecimal;

public class Demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
/*
		BigDecimal类的概述和方法使用
		1.BigDecimal的概述
			由于在运算的时候,float类型和double很容易丢失精度。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
		
		2.构造方法
		public BigDecimal(String val)
		
		3.成员方法
		public BigDecimal add(BigDecimal augend)
		public BigDecimal subtract(BigDecimal subtrahend)
		public BigDecimal multiply(BigDecimal multiplicand)
		public BigDecimal divide(BigDecimal divisor)*/

		//System.out.println(2.0 - 1.1);

		BigDecimal bd1 = new BigDecimal("2.0");
		BigDecimal bd2 = new BigDecimal("1.1");
		
		//+ 运算
		BigDecimal sum = bd1.add(bd2);
		System.out.println(sum);
		
		//- 运算
		BigDecimal subtract = bd1.subtract(bd2);
		System.out.println(subtract);
		
		//* 运算
		BigDecimal multi = bd1.multiply(bd2);
		System.out.println(multi);
		
		// / 运算
		BigDecimal bd3 = new BigDecimal("1.0");
		BigDecimal bd4 = new BigDecimal("0.25");
		
		/***
		 * 在使用BigDecimal做除法运算时,这两个数要能被除尽,否则就会报错
		 */
		BigDecimal divide = bd3.divide(bd4);
		System.out.println(divide);
		
		//System.out.println( 1 / 0.3);
		//借了1000块钱,分3个月还,每月还多少本本金?
		System.out.println(1000 / 3.0);//1000 - 333.33 * 2
				
	}
}

13.Date类

日期类
Date date = new Date();//表示当前的时间
date.getTime();方法返回的自1970-1-1到现在走的毫秒数(时间戳)
掌握把毫秒数 转成 Date
long time = 123412341234123L;
Date date = new Date(time);

14.SimpleDateFormat

SimpleDateFormat是DateFormat的子类,DateFormat是一个抽象类
SimpleDateFormat把 Date 转成固定格式的字符串时间
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String str = sdf.format(date);
SimpleDateFormat把 字符串时间 转成 Date
String str = “2018-02-02 17:00:23”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date = sdf.parser(str);

12.Calendar 类

Calendar是一个日历类
Calendar也是一个抽象类,用它子类,创建子类对象时,不是用new GregorianCalendar
一般通过Calendar类方法获取
Calendar calendar = Calendar.getInstance();

Calendar可以获取年月日时分秒星期的数字
calendar.get(Calendar.YEAR);

Calendar的add方法 : 用于给指定字段添加一个值
calendar.add(Calendar.YEAR,1);//给年加1

Calendar.set方法:指定日历的年月日
calendar.set(2016,2,3)//设置2016年,3月,3号

练习题

1.计算自己来到这个世界多少天

**思路:**计算出生日那天 跟 现在的 毫秒的差值

public static void main(String[] args) throws ParseException {
		//课堂案例:计算自己来到这个世界多少天
		
		String birthday = "1992-01-02";
		String now = "2018-02-02";
		
		//思路?计算两个时间的毫秒差
		
		//1.把前面两个日期转成 Date类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = sdf.parse(birthday);
		Date date2 = sdf.parse(now);
		
		//2.算毫秒差
		long delta = date2.getTime() - date1.getTime();
		
		//3.算天数
		long day = delta / 1000 / 60 / 60 / 24;
		System.out.println("我来到这个世界" + day + "天");

	}

2.如何判断是平年还是闰年

**思路:**获取2月份有多少天

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int year = 2020;
		
		//获取一个日历对象
		Calendar calendar = Calendar.getInstance();
		
		//设置日历为2016年3月1号
		calendar.set(year, 2, 1);
		
		//在日期的字段减1
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		
		
		//求2月有几天?
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		if(day == 29){
			System.out.println(year + " 是闰年");
		}else{
			System.out.println(year + " 是平年");
		}
		//System.out.println(day);

	}

面试题

1.自动装箱面试题

  • 自动装箱,如果值一样,地址也一样(不严谨)
  • 自动装箱,范围在-128 ~ 127 【256个数】的地址一样,其它范围地址不一样
  • 因为内部有个Integer的缓冲池
/**
 * 考点Integer内部装箱的实现
 *
 */
public class Demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//看程序写结果		
		Integer i1 = new Integer(97);
		Integer i2 = new Integer(97);
		System.out.println(i1 == i2);//false
		System.out.println(i1.equals(i2));//true
		System.out.println("-----------");
	
		Integer i3 = new Integer(197);
		Integer i4 = new Integer(197);
		System.out.println(i3 == i4);//false
		System.out.println(i3.equals(i4));//true
		System.out.println("-----------");

		Integer i5 = -127;//自动装箱
		Integer i6 = -127;
		/**
		 * 自动装箱,如果值一样,地址也一样
		 * 自动装箱,范围在-128 ~ 127 【256个数】的地址一样,其它范围地址不一样
		 * 因为内部有个Integer的缓冲池
		 */
		System.out.println(i5 == i6);//true ?
		System.out.println(i5.equals(i6));//true
		System.out.println("-----------");

		Integer i7 = -129;
		Integer i8 = -129;
		System.out.println(i7 == i8);//true
		System.out.println(i7.equals(i8));//true
		
		Double d;

	}

}

2.冒泡排序

3.选择排序

4.二分查找

总结

今天学了三个算法,冒泡,选择,二分查找。会用Arrays的三个工具类(toString、sort、binarySearch)进行输出字符数组、排序、二分查找。知道了基本数据类型的包装类的使用以及来由;知道了自动拆装箱的使用,以及对内部的缓冲池有了进一步的了解,知道了范围是-128~127才能保证Interger 类型对象地址一样;然后就是几个常用的Math、Random、System工具类的使用。会灵活的用SimpleDateFormat处理日期格式,掌握了format()、parser()方法的使用,最后学了Calender,掌握了add()、set()方法。敲代码是一种态度!

发布了24 篇原创文章 · 获赞 4 · 访问量 618

猜你喜欢

转载自blog.csdn.net/qq_43488797/article/details/103759116