文章目录
1.API
API概述:
- API(Application Programming Interface):应用程序编程接口
Java API:指的就是 JDK 中提供的各种功能的 Java 类
这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用
API 使用练习:
需求:按照帮助文档的使用步骤学习 Scanner 类的使用,并实现键盘录入一个字符串,最后输出结果
package com.company;
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println("请输入一个字符串数据:");
// String line = sc.nextLine();
//ctrl+alt+v
String line = sc.nextLine();
//输出结果
System.out.println("你输入的数据是:" + line);
}
}
2.String
String概述:
String 类在 java.lang 包下,所以使用的时候不需要导包
String 类代表字符串,Java 程序中的所有字符串字面值(如 "abc"
)都作为此类的实例实现。
也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象
字符串的特点:
- 字符串不可变,他们的值在创建后不能被更改
- 虽然 String 的值是不可变的,但是他们可以被共享
- 字符串效果上相当于字符数组(char[]),但是底层原理是字节数组(byte[])
2.1 String 构造方法
推荐使用直接赋值的方式得到字符串对象
package com.company_02;
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.2 String 对象的特点
- 通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址不同
- 以 “” 方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在字符串池中维护
2.3 字符串的比较
使用 == 作比较:
- 基本类型:比较的是数据值是否相同
- 引用类型:比较的是地址值是否相同
字符串是对象,他比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals()
- public boolean equals(Object anObject): 将此字符串与指定对象进行比较,由于我们比较的是字符串对象,所以参数直接传递一个字符串
package com.company_02;
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));
}
}
案例:(用户登录)
需求:已知用户名和密码,请使用程序实现模拟用户登录,总共给三次机会,登录之后,给出相应的提示
package com.company_02;
import java.util.Scanner;
public class StringTest01 {
public static void main(String[] args) {
String username = "xiaoxin";
String password = "123456";
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) + "次机会");
}
}
}
}
}
案例:(遍历字符串)
需求:键盘录入一个字符串,使用程序实现在控制台遍历该字符串
遍历字符串,首先能够获取到字符串的每一个字符
- public char charAt(int index); 返回指定索引处的char值,字符串的索引也是从0开始
遍历字符串,其次要获取到字符串的长度
- public int length(); 返回此字符串的长度
- 数组的长度:数组名.length
- 字符串的长度:字符串对象.length()
遍历字符串的通用格式:
for (int i = 0; i < s.length(); i++) {
s.charAt(i) //就是指定索引处的字符值
}
package com.company_02;
import java.util.Scanner;
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));
}
}
}
案例:(统计字符次数)
需求:键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
假如ch是一个字符
- 判断大写字母:ch>=‘A’ && ch<=‘Z’
- 判断小写字母:ch>=‘a’ && ch<=‘z’
- 判断数字:ch>=‘0’ && ch<=‘9’
package com.company_02;
import java.util.Scanner;
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);
//判断ch属于什么类型
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 + "个");
}
}
案例:(拼接字符串)
需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果
例如:数组 int[] arr = {1,2,3}; 执行方法后的输出结果为:[1,2,3]
package com.company_02;
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;
}
}
案例:(字符串反转)
需求:定义一个方法,实现字符串反转,键盘录入一个字符串,调用该方法后,在控制台输出结果
例如:键盘录入abc,输出结果cba
package com.company_02;
import java.util.Scanner;
public class StringTest05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
String s = revers(line);
System.out.println("s:" + s);
}
public static String revers(String s) {
String ss = "";
for (int i = s.length() - 1; i >= 0; i--) {
ss += s.charAt(i);
}
return ss;
}
}
3. StringBuilder
如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费内存空间,而这种操作还不可避免,那么有没有一种比较好的方式可以解决这个问题呢?答案是肯定的,我们可以通过Java提供的StringBuilder类就来解决这个问题
StringBuilder是一个可变的字符串类,我们可以把它看成是一个容器
这里的可变指的是StringBuilder对象中的内容是可变的
String和StringBuilder的区别:
- String:内容时不可变的
- StringBuilder:内容是可变的
package com.company_03;
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("xiaoxin");
System.out.println("sb2:"+ sb);
System.out.println("sb2.length():"+sb2.length());
}
}
3.1 StringBuilder的添加和反转方法
package com.company_03;
public class StringBuilderDemo02 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// sb.append("hello");
// sb.append("world");
// sb.append("java");
// sb.append(100);
// System.out.println("sb:" + sb);
//链式编程
sb.append("hello").append("world").append("java").append(100);
System.out.println("sb:" + sb);
sb.reverse();
System.out.println("sb:" + sb);
}
}
3.2StringBuilder 和 string相互转换
- StringBuilder 转换为 String
- public String toString(): 通过toString() 就可以实现把StringBuilder转换为String
- String 转换为 StringBuilder
- pubilc StringBuilder(String s): 通过构造方法就可以实现把String转换为StringBuilder
package com.company_03;
public class StringBuilderDemo03 {
public static void main(String[] args) {
/*
StringBuilder sb = new StringBuilder();
sb.append("xiaoxin");
String s = sb.toString();
System.out.println(s);
*/
String s = "xiaoxin";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
}
}
案例:(拼接字符串)
需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果
例如:数组int[] arr = {1,2,3}; 执行方法后的输出结果为:[1,2,3]
package com.company_03;
public class StringBuilderTest01 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String s = arrayToString(arr);
System.out.println(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;
}
}
案例:(字符串反转)
需求:定义一个方法,实现字符串反转,键盘录入一个字符串,调用该方法后,在控制台输出结果
例如:键盘录入abc,输出结果cba
package com.company_03;
import java.util.Scanner;
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) {
// StringBuilder sb = new StringBuilder(s);
// sb.reverse();
// String ss = sb.toString();
// return ss;
return new StringBuilder(s).reverse().toString();
}
}