文章目录
- 1.API
-
- 2. String类
- 2.1 String类概述
- 2.2 String类的特点
- 2.3 String类的构造方法
- 2.4 创建字符串对象两种方式的区别
- 2.5 字符串的比较
- 2.5.1 ==号的作用
- 2.5.2 equals方法的作用
- 2.6 用户登录案例
-
- 2.7 遍历字符串案例
-
- 2.8 统计字符次数案例
-
- 2.9 字符串拼接案例
-
- 2.10 字符串反转案例
-
- 2.11 帮助文档查看String常用方法
- 3. StringBuilder类
- 3.1 StringBuilder类概述
- 3.2 StringBuilder类和String类的区别
- 3.3 StringBuilder类的构造方法
- 3.4 StringBuilder类添加和反转方法
- 3.5 StringBuilder和String相互转换
- 3.6 字符串拼接升级版案例
-
- 3.7 字符串反转升级版案例
-
- 3.8 帮助文档查看StringBuilder常用方法
- 4. Math
- 4.1 Math类概述
- 4.2 Math中方法的调用方式
- 4.3 Math类的常用方法
- 5. System
-
- 6. Object
- 6.1 Object类概述
- 6.2 toString方法
- 6.3 Object类的equals方法
- 7 冒泡排序原理
-
- 8 Arrays
- 8.1 Arrays的常用方法
- 8.2 工具类设计思想
- 9.包装类
- 9.1 基本类型包装类
- 9.2 Integer类
- 9.3 int和String类型的相互转换
- 9.4 字符串数据排序案例
- 9.5 自动拆箱和自动装箱
- 10. 时间日期类
- 10.1 Date类
- 10.2 Date类常用方法
- 10.3 SimpleDateFormat类
- 10.4 日期工具类案例
- 10.5 Calendar类
- 10.6 二月天案例
1.API
1.1 API概述
1.2 如何使用API帮助文档
2. String类
2.1 String类概述
String 类代表字符串,Java 程序中的所有字符串文字(例如“abc”)都被实现为此类的实例。也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象。String 类在 java.lang 包下,所以使用的时候不需要导包!
2.2 String类的特点
- 字符串不可变,它们的值在创建后不能被更改
- 虽然 String 的值是不可变的,但是它们可以被共享
- 字符串效果上相当于字符数组( char[] ),但是底层原理是字节数组( byte[] )
2.3 String类的构造方法
-
常用的构造方法
方法名 | 说明 |
---|
public String() | 创建一个空白字符串对象,不含有任何内容 |
public String(char[] chs) | 根据字符数组的内容,来创建字符串对象 |
public String(byte[] bys) | 根据字节数组的内容,来创建字符串对象 |
String s = “abc”; | 直接赋值的方式创建字符串对象,内容就是abc |
-
示例代码
public class StringDemo01 {
public static void main(String[] args) {
String s1 = new String();
System.out.println("s1:" + s1);
char[] chs = {'a', 'b', 'c'};
String s2 = new String(chs);
System.out.println("s2:" + s2);
byte[] bys = {97, 98, 99};
String s3 = new String(bys);
System.out.println("s3:" + s3);
String s4 = "abc";
System.out.println("s4:" + s4);
}
}
2.4 创建字符串对象两种方式的区别
2.5 字符串的比较
2.5.1 ==号的作用
- 比较基本数据类型:比较的是具体的值
- 比较引用数据类型:比较的是对象地址值
2.5.2 equals方法的作用
-
方法介绍
public boolean equals(String s) 比较两个字符串内容是否相同、区分大小写
-
示例代码
public class StringDemo02 {
public static void main(String[] args) {
char[] chs = {'a', 'b', 'c'};
String s1 = new String(chs);
String s2 = new String(chs);
String s3 = "abc";
String s4 = "abc";
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s3 == s4);
System.out.println("--------");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
}
}
2.6 用户登录案例
2.6.1 案例需求
已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示
2.6.2 代码实现
public class StringTest01 {
public static void main(String[] args) {
String username = "itheima";
String password = "czbk";
for(int i=0; i<3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
if (name.equals(username) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if(2-i == 0) {
System.out.println("你的账户被锁定,请与管理员联系");
} else {
System.out.println("登录失败,你还有" + (2 - i) + "次机会");
}
}
}
}
}
2.7 遍历字符串案例
2.7.1 案例需求
键盘录入一个字符串,使用程序实现在控制台遍历该字符串
2.7.2 代码实现
public class StringTest02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
for(int i=0; i<line.length(); i++) {
System.out.println(line.charAt(i));
}
}
}
2.8 统计字符次数案例
2.8.1 案例需求
键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
2.8.2 代码实现
public class StringTest03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
for(int i=0; i<line.length(); i++) {
char ch = line.charAt(i);
if(ch>='A' && ch<='Z') {
bigCount++;
} else if(ch>='a' && ch<='z') {
smallCount++;
} else if(ch>='0' && ch<='9') {
numberCount++;
}
}
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字:" + numberCount + "个");
}
}
2.9 字符串拼接案例
2.9.1 案例需求
定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,
并在控制台输出结果。例如,数组为 int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3]
2.9.2 代码实现
public class StringTest04 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String s = arrayToString(arr);
System.out.println("s:" + s);
}
public static String arrayToString(int[] arr) {
String s = "";
s += "[";
for(int i=0; i<arr.length; i++) {
if(i==arr.length-1) {
s += arr[i];
} else {
s += arr[i];
s += ", ";
}
}
s += "]";
return s;
}
}
2.10 字符串反转案例
2.10.1 案例需求
定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果
例如,键盘录入 abc,输出结果 cba
2.10.2 代码实现
public class StringTest05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
String s = reverse(line);
System.out.println("s:" + s);
}
public static String reverse(String s) {
String ss = "";
for(int i=s.length()-1; i>=0; i--) {
ss += s.charAt(i);
}
return ss;
}
}
2.11 帮助文档查看String常用方法
方法名 | 说明 |
---|
public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写(用户名和密码) |
public char charAt(int index) | 返回指定索引处的 char 值 |
public int length() | 返回此字符串的长度 |
3. StringBuilder类
3.1 StringBuilder类概述
StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器,这里的可变指的是 StringBuilder 对象中的内容是可变的
3.2 StringBuilder类和String类的区别
- String类:内容是不可变的
- StringBuilder类:内容是可变的
3.3 StringBuilder类的构造方法
-
常用的构造方法
方法名 | 说明 |
---|
public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 |
public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 |
-
示例代码
public class StringBuilderDemo01 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
System.out.println("sb:" + sb);
System.out.println("sb.length():" + sb.length());
StringBuilder sb2 = new StringBuilder("hello");
System.out.println("sb2:" + sb2);
System.out.println("sb2.length():" + sb2.length());
}
}
3.4 StringBuilder类添加和反转方法
-
添加和反转方法
方法名 | 说明 |
---|
public StringBuilder append(任意类型) | 添加数据,并返回对象本身 |
public StringBuilder reverse() | 返回相反的字符序列 |
-
示例代码
public class StringBuilderDemo01 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world").append("java").append(100);
System.out.println("sb:" + sb);
sb.reverse();
System.out.println("sb:" + sb);
}
}
3.5 StringBuilder和String相互转换
public class StringBuilderDemo02 {
public static void main(String[] args) {
String s = "hello";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
}
}
3.6 字符串拼接升级版案例
3.6.1 案例需求
定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,
并在控制台输出结果。例如,数组为int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3]
3.6.2 代码实现
public class StringBuilderTest01 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String s = arrayToString(arr);
System.out.println("s:" + s);
}
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;
}
}
3.7 字符串反转升级版案例
3.7.1 案例需求
定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果
例如,键盘录入abc,输出结果 cba
3.7.2 代码实现
public class StringBuilderTest02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
String s = myReverse(line);
System.out.println("s:" + s);
}
public static String myReverse(String s) {
return new StringBuilder(s).reverse().toString();
}
}
3.8 帮助文档查看StringBuilder常用方法
方法名 | 说明 |
---|
public StringBuilder append (任意类型) | 添加数据,并返回对象本身 |
public StringBuilder reverse() | 返回相反的字符序列 |
public int length() | 返回长度,实际存储值 |
public String toString() | 通过toString()就可以实现把StringBuilder转换为String |
4. Math
4.1 Math类概述
4.2 Math中方法的调用方式
- Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
4.3 Math类的常用方法
方法名 方法名 | 说明 |
---|
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 返回大于或等于参数的最小double值,等于一个整数 |
public static double floor(double a) | 返回小于或等于参数的最大double值,等于一个整数 |
public static int round(float a) | 按照四舍五入返回最接近参数的int |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow (double a,double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0,1.0) |
5. System
5.1 System类的常用方法
方法名 | 说明 |
---|
public static void exit(int status) | 终止当前运行的 Java 虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
6. Object
6.1 Object类概述
- Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
6.2 toString方法
-
查看方法源码的方式
-
重写toString方法的方式
- Alt + Insert 选择toString
- 在类的空白区域,右键 -> Generate -> 选择toString
-
toString方法的作用:
-
示例代码:
public class Student extends Object {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("林青霞");
s.setAge(30);
System.out.println(s);
System.out.println(s.toString());
}
-
运行结果:
Student{name='林青霞', age=30}Student{name='林青霞', age=30}
6.3 Object类的equals方法
-
equals方法的作用
- 用于对象之间的比较,返回true和false的结果
- 举例:s1.equals(s2); s1和s2是两个对象
-
重写equals方法的场景
- 不希望比较对象的地址值,想要结合对象属性进行比较的时候。
-
重写equals方法的方式
-
- alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
-
- 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。
-
示例代码:
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("林青霞");
s1.setAge(30);
Student s2 = new Student();
s2.setName("林青霞");
s2.setAge(30);
System.out.println(s1.equals(s2));
}
}
7 冒泡排序原理
- 冒泡排序概述
- 一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序
- 如果有n个数据进行排序,总共需要比较n-1次
- 每一次比较完毕,下一次的比较就会少一个数据参与
7.1 冒泡排序代码实现
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13};
System.out.println("排序前:" + arrayToString(arr));
for(int x=0; x<arr.length-1; x++) {
for(int i=0; i<arr.length-1-x; 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;
}
}
8 Arrays
8.1 Arrays的常用方法
方法名 | 说明 |
---|
public static String toString(int[] a) | 返回指定数组的内容的字符串表示形式 |
public static void sort(int[] a) | 按照数字顺序排列指定的数组 |
8.2 工具类设计思想
1、构造方法用 private 修饰
2、成员用 public static 修饰
9.包装类
9.1 基本类型包装类
9.2 Integer类
9.3 int和String类型的相互转换
-
int转换为String
-
String转换为int
9.4 字符串数据排序案例
-
案例需求
有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”
-
代码实现
public class IntegerTest {
public static void main(String[] args) {
String s = "91 27 46 38 50";
String[] strArray = s.split(" ");
int[] arr = new int[strArray.length];
for(int i=0; i<arr.length; i++) {
arr[i] = Integer.parseInt(strArray[i]);
}
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for(int i=0; i<arr.length; i++) {
if(i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(" ");
}
}
String result = sb.toString();
System.out.println(result);
}
}
9.5 自动拆箱和自动装箱
10. 时间日期类
10.1 Date类
10.2 Date类常用方法
10.3 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):从给定字符串的开始解析文本以生成日期
-
示例代码
public class SimpleDateFormatDemo {
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);
System.out.println("--------");
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);
}
}
10.4 日期工具类案例
-
案例需求
定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法
-
代码实现
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;
}
}
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);
String s3 = DateUtils.dateToString(d, "HH:mm:ss");
System.out.println(s3);
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);
}
}
10.5 Calendar类
-
Calendar类概述
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 CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
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 + "日");
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}
10.6 二月天案例
-
案例需求
获取任意一年的二月有多少天
-
代码实现
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(year + "年的2月份有" + date + "天");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)