4.1 字符串处理

字符串是Java中常用的数据类型,用于表示和操作字符序列。Java提供了丰富的字符串处理类。

4.1.1 创建字符串

  1. String str1 = "Hello, World!";
  2. String str2 = new String("Hello, World!");

4.1.2 字符串方法

获取字符串长度

  1. String str = "Hello, World!";
  2. int length = str.length();
  3. System.out.println("Length: " + length); // 输出:Length: 13

字符串拼接

  1. String firstName = "John";
  2. String lastName = "Doe";
  3. String fullName = firstName + " " + lastName;
  4. System.out.println("Full Name: " + fullName); // 输出:Full Name: John Doe

字符串比较

  1. String str1 = "Hello";
  2. String str2 = "hello";
  3. boolean isEqual = str1.equals(str2);
  4. boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
  5. System.out.println("isEqual: " + isEqual); // 输出:isEqual: false
  6. System.out.println("isEqualIgnoreCase: " + isEqualIgnoreCase); // 输出:isEqualIgnoreCase: true

查找子字符串

  1. String str = "Hello, World!";
  2. int index = str.indexOf("World");
  3. System.out.println("Index of 'World': " + index); // 输出:Index of 'World': 7

截取子字符串

  1. String str = "Hello, World!";
  2. String subStr = str.substring(7, 12);
  3. System.out.println("Sub String: " + subStr); // 输出:Sub String: World

转换大小写

  1. String str = "Hello, World!";
  2. String upperStr = str.toUpperCase();
  3. String lowerStr = str.toLowerCase();
  4. System.out.println("Upper Case: " + upperStr); // 输出:Upper Case: HELLO, WORLD!
  5. System.out.println("Lower Case: " + lowerStr); // 输出:Lower Case: hello, world!

4.2 集合框架:List、Set、Map

Java集合框架提供了一组接口和类,用于存储和操作一组数据。

4.2.1 List接口

List接口表示有序的元素集合,可以包含重复的元素。常用的实现类有ArrayListLinkedList

ArrayList示例

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Main {
  4. public static void main(String[] args) {
  5. List<String> fruits = new ArrayList<>();
  6. fruits.add("Apple");
  7. fruits.add("Banana");
  8. fruits.add("Orange");
  9. System.out.println("Fruits: " + fruits); // 输出:Fruits: [Apple, Banana, Orange]
  10. fruits.remove("Banana");
  11. System.out.println("After removal: " + fruits); // 输出:After removal: [Apple, Orange]
  12. String firstFruit = fruits.get(0);
  13. System.out.println("First Fruit: " + firstFruit); // 输出:First Fruit: Apple
  14. }
  15. }

4.2.2 Set接口

Set接口表示不包含重复元素的集合。常用的实现类有HashSetTreeSet

HashSet示例

  1. import java.util.HashSet;
  2. import java.util.Set;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Set<String> fruits = new HashSet<>();
  6. fruits.add("Apple");
  7. fruits.add("Banana");
  8. fruits.add("Orange");
  9. fruits.add("Apple"); // 重复添加不会生效
  10. System.out.println("Fruits: " + fruits); // 输出:Fruits: [Apple, Banana, Orange]
  11. }
  12. }

4.2.3 Map接口

Map接口表示键值对映射。常用的实现类有HashMapTreeMap

HashMap示例

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Map<String, Integer> fruitPrices = new HashMap<>();
  6. fruitPrices.put("Apple", 3);
  7. fruitPrices.put("Banana", 2);
  8. fruitPrices.put("Orange", 5);
  9. System.out.println("Fruit Prices: " + fruitPrices); // 输出:Fruit Prices: {Apple=3, Banana=2, Orange=5}
  10. int applePrice = fruitPrices.get("Apple");
  11. System.out.println("Price of Apple: " + applePrice); // 输出:Price of Apple: 3
  12. fruitPrices.remove("Banana");
  13. System.out.println("After removal: " + fruitPrices); // 输出:After removal: {Apple=3, Orange=5}
  14. }
  15. }

4.3 异常处理:失败的艺术

异常处理是指程序在运行过程中出现错误时的处理机制。Java通过try-catch块来处理异常。

4.3.1 基本语法

try-catch

  1. public class Main {
  2. public static void main(String[] args) {
  3. try {
  4. int result = 10 / 0; // 可能抛出ArithmeticException
  5. System.out.println("Result: " + result);
  6. } catch (ArithmeticException e) {
  7. System.out.println("Error: Division by zero is not allowed."); // 输出:Error: Division by zero is not allowed.
  8. }
  9. }
  10. }

try-catch-finally

  1. public class Main {
  2. public static void main(String[] args) {
  3. try {
  4. int result = 10 / 0; // 可能抛出ArithmeticException
  5. System.out.println("Result: " + result);
  6. } catch (ArithmeticException e) {
  7. System.out.println("Error: Division by zero is not allowed."); // 输出:Error: Division by zero is not allowed.
  8. } finally {
  9. System.out.println("This block is always executed."); // 输出:This block is always executed.
  10. }
  11. }
  12. }

4.3.2 自定义异常

可以通过继承Exception类来创建自定义异常。

  1. class CustomException extends Exception {
  2. public CustomException(String message) {
  3. super(message);
  4. }
  5. }
  6. public class Main {
  7. public static void main(String[] args) {
  8. try {
  9. throw new CustomException("This is a custom exception.");
  10. } catch (CustomException e) {
  11. System.out.println("Caught custom exception: " + e.getMessage()); // 输出:Caught custom exception: This is a custom exception.
  12. }
  13. }
  14. }