黑马全套Java教程(一)
黑马全套Java教程(二)
黑马全套Java教程(三)
黑马全套Java教程(四)
黑马全套Java教程(五)
黑马全套Java教程(六)
本博客从视频d198开始,视频链接
27 Arrays
27.1 冒泡排序
package myArrays.Demo1;
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13};
System.out.println("排序前:" + arrayToString(arr));
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
System.out.println("排序后:" + arrayToString(arr));
}
public static String arrayToString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}
27.2 Arrays类的概述和常用方法
工具类的设计思想:
- 构造方法用private修饰
- 成员用public static修饰
package myArrays.Demo2;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13};
System.out.println("排序前:" + Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
}
}
28 基本类型包装类
28.1 基本类型包装类概述
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换
package myInteger.Demo1;
public class InterDemo {
public static void main(String[] args) {
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}
}
28.2 Integer类的概述和使用
Integer:包装一个对象中的原始类型int的值
package myInteger.Demo2;
public class InterDemo {
public static void main(String[] args) {
Integer i1 = new Integer(100);
System.out.println(i1);
Integer i2 = new Integer("100");
System.out.println(i2);
Integer i3 = Integer.valueOf(100);
System.out.println(i3);
Integer i4 = Integer.valueOf("100");
System.out.println(i4);
}
}
28.3 int和String的相互转换
基本类型包装类的最常见操作就是:用于基本类型和字符串之间的相互转换
package myInteger.Demo3;
public class IntegerDemo {
public static void main(String[] args) {
int number = 100;
String s1 = "" + number;
System.out.println(s1);
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("--------------");
String s = "100";
Integer i = Integer.valueOf(s);
int x = i.intValue();
System.out.println(x);
int y = Integer.parseInt(s);
System.out.println(y);
}
}
案例:字符串中数据排序
package myInteger.Demo4;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
String s = "91 27 46 38 50";
String[] strArray = s.split(" ");
String start = Arrays.toString(strArray);
System.out.println(start);
int[] arr = new int[strArray.length];
for(int i=0;i< strArray.length ;i++){
arr[i] = Integer.parseInt(strArray[i]);
}
Arrays.sort(arr);
String result = Arrays.toString(arr);
System.out.println(result);
}
}
28.4 自动装箱和拆箱
装箱:把基本数据类型转换为对应的包装类类型 拆箱:把包装类类型转换为对应的基本数据类型
package myInteger.Demo5;
import myInteger.Demo1.InterDemo;
public class IntegerDemo {
public static void main(String[] args) {
Integer i = Integer.valueOf(100);
Integer ii = 100;
ii = ii.intValue() + 200;
ii += 200;
System.out.println(ii);
Integer iii = null;
if(iii!=null) {
iii += 300;
}
System.out.println(iii);
}
}
29 日期类
29.1 Date类概述和构造方法
Date代表了一个特定的时间,精确到毫秒
package myDate.Demo1;
import java.util.Date;
public class DateDemo01 {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println(d1);
long date = 1000*60*60;
Date d2 = new Date(date);
System.out.println(d2);
}
}
常用方法:
package myDate.Demo1;
import java.util.Date;
public class DateDemo02 {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
System.out.println(d.getTime());
System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
System.out.println("------------------");
long time = System.currentTimeMillis();
d.setTime(time);
System.out.println(d);
}
}
29.2 SimpleDateFormat类概述
构造方法: 格式化和解析日期:
package mySimpleDateFormat.Demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo1 {
public static void main(String[] args) throws ParseException {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
String ss = "2048-08-09 11:11:11";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
案例:日期工具类 DateUtils.java
package mySimpleDateFormat.Demo2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
private DateUtils(){}
public static String dateToString(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(date);
return s;
}
public static Date stringToDate(String s, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(s);
return d;
}
}
DateDemo.java
package mySimpleDateFormat.Demo2;
import java.text.ParseException;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
Date d = new Date();
String s1 = DateUtils.dateToString(d, "yyyy年MM月dd日 HH:mm:ss");
System.out.println(s1);
String s2 = DateUtils.dateToString(d, "yyyy年MM月dd日");
System.out.println(s2);
System.out.println("-----------------");
String s = "2048-08-09 12:12:12";
Date dd = DateUtils.stringToDate(s, "yyyy-MM-dd HH:mm:ss");
System.out.println(dd);
}
}
29.3 Calendar概述 和 常用方法
package myCalendar.Demo1;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}
package myCalendar.Demo2;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
System.out.println("--------------");
c.set(2048,10,10);
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH) + 1;
int date1 = c.get(Calendar.DATE);
System.out.println(year1 + "年" + month1 + "月" + date1 + "日");
System.out.println("---------------");
c.add(Calendar.YEAR, 3);
c.add(Calendar.DATE, -5);
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH) + 1;
int date2 = c.get(Calendar.DATE);
System.out.println(year2 + "年" + month2 + "月" + date2 + "日");
}
}
案例:二月天
package myCalendar.Demo3;
import java.util.Calendar;
import java.util.Scanner;
public class CalendarTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
Calendar c = Calendar.getInstance();
c.set(year, 2, 1);
c.add(Calendar.DATE, -1);
int date = c.get(Calendar.DATE);
System.out.println(date);
}
}
30 异常
30.1 异常概述
异常:就是程序出现了不正常的情况 索引越界异常显示:
30.2 JVM的默认处理方案
如果程序出现了问题,我们没有做任何处理,最终JVM会做默认的处理
- 把异常的名称,异常原因及异常出现的位置等信息输出在了控制台
- 程序停止执行
30.3 异常处理
如果程序出现了问题,我们需要自己来处理,有两种方案:
try…catch…
类似Python的try…except…
package myException.Demo2;
public class Demo1 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
throws
1. 2. 3. 4.
30.4 Throwable的成员方法
package myException.Demo2;
public class Demo2 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("e.getMessage():" + e.getMessage());
System.out.println("e.toString():" + e.toString());
}
}
}
30.5 编译时异常和运行时异常区别
30.6 自定义异常
ScoreException.java
package myException.Demo4;
public class ScoreException extends Exception{
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
Teacher.java
package myException.Demo4;
public class Teacher {
public void cheakScore(int score) throws ScoreException {
if (score < 0 || score > 100) {
throw new ScoreException("你给的分数有误,分数应该在0-100之间");
} else {
System.out.println("分数正常!");
}
}
}
TeacherTest.java
package myException.Demo4;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) throws ScoreException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入分数:");
int score = sc.nextInt();
Teacher t = new Teacher();
t.cheakScore(score);
}
}
注意: 本博客到视频d219截止!
|