我最近透過[https://exercism.org/tracks/java/exercises]中的實作學習了Java。我目前的進度是 148 次練習中的 13 次。我想分享我所學到的東西。
這篇文章介紹了我對 .split()、.trim()、.isDigit()、.isLetter()、Comparable
定義:.split()方法依分隔符號[1]將String分割成陣列。
文法:
public String[] split(String regex, int limit)
參數:
範例:
public class Main{ public void getProductPrice(String products){ double totalPrice = 0.0; StringBuilder priceDetails = new StringBuilder(); String[] singleProduct = products.split("; "); for(int i = 0; i < singleProduct.length; i++){ String[] productInfo = singleProduct[i].split(", "); totalPrice += Double.parseDouble(productInfo[2]); priceDetails.append(productInfo[2]); if(i < singleProduct.length - 1){ priceDetails.append(" + "); } } System.out.println(priceDetails + " = " + totalPrice); } public static void main(String arg[]){ Main obj = new Main(); obj.getProductPrice("1, dragonfruit, 12.50; 2, guava, 23.45; 3, avocado, 395.67"); } }
輸出:
12.50 + 23.45 + 395.67 = 431.62
定義:.trim() 方法刪除字串兩端的空格 [2]。
文法:
public String trim()
參數:
範例:
public class Main{ public static void main(String args[]){ String str = " You can do it! "; System.out.println(str); System.out.println(str.trim()); } }
輸出:
You can do it! You can do it!
定義:.isDigit() 方法判斷字元是否為數字 [3].
文法:
public static boolean isDigit(char ch)
參數:
範例:
public class Main{ // return true when the given parameter has a digit public boolean searchDigit(String str){ for(int i = 0; i < str.length(); i++){ // charAt() method returns the character at the specified index in a string if(Character.isDigit(str.charAt(i))){ return true; } } return false; } // print digit index and value public void digitInfo(String str){ for(int i = 0; i < str.length(); i++){ if(Character.isDigit(str.charAt(i))){ System.out.println("Digit: " + str.charAt(i) + " found at index " + i); } } } public static void main(String args[]){ Main obj = new Main(); String[] strList = {"RT7J", "1EOW", "WBJK"}; for(String str : strList){ if(obj.searchDigit(str)){ obj.digitInfo(str); }else{ System.out.println("No digit"); } } } }
輸出:
Digit: 7 found at index 2 Digit: 1 found at index 0 No digit
定義:.isLetter() 方法決定字元是否為字母 [4].
文法:
public static boolean isLetter(char ch)
參數:
範例:
public class Main{ // check whether phoneNum has letter public void searchLetter(String phoneNum){ boolean hasLetter = false; for(int i = 0; i < phoneNum.length(); i++){ if(Character.isLetter(phoneNum.charAt(i))){ hasLetter = true; // return letter value and index System.out.println(phoneNum + " has letter '" + phoneNum.charAt(i) + "' at index " + i); } } // phone number is valid when no letter if(!hasLetter){ System.out.println(phoneNum + " is valid"); } System.out.println(); } public static void main(String args[]){ Main obj = new Main(); String[] phoneNum = {"A0178967547", "0126H54786K5", "0165643484"}; for(String item: phoneNum){ obj.searchLetter(item); } } }
輸出:
A0178967547 has letter 'A' at index 0 0126H54786K5 has letter 'H' at index 4 0126H54786K5 has letter 'K' at index 10 0165643484 is valid
定義:可比較的
範例:
// file: Employee.java public class Employee implements Comparable<Employee>{ private String email; private String name; private int age; public Employee(String email, String name, int age){ this.email = email; this.name = name; this.age = age; } // The Comparable interface has a method called compareTo(T obj). // This method helps decide how to order objects, so they can be sorted in a list @Override public int compareTo(Employee emp){ // compare age: // return this.age - emp.age; // (this.age - emp.age) = negative value means this.age before emp.age; // (this.age - emp.age) = positive means this.age after emp.age // compare email: return this.email.compareTo(emp.email); } @Override public String toString(){ return "[email=" + this.email + ", name=" + this.name + ", age=" + this.age +"]"; } }
// file: Main.java import java.util.Arrays; public class Main { public static void main(String args[]){ Employee[] empInfo = new Employee[3]; empInfo[0] = new Employee("joseph@gmail.com", "Joseph", 27); empInfo[1] = new Employee("alicia@gmail.com", "Alicia", 30); empInfo[2] = new Employee("john@gmail.com", "John", 24); Arrays.sort(empInfo); System.out.println("After sorting:\n" + Arrays.toString(empInfo)); } }
輸出:
After sorting: [[email=alicia@gmail.com, name=Alicia, age=30], [email=john@gmail.com, name=John, age=24], [email=joseph@gmail.com, name=Joseph, age=27]]
定義:使用者定義的 Java 異常是開發人員為處理特定錯誤條件而建立的自訂異常 [6]。
範例:
public String[] split(String regex, int limit)
public class Main{ public void getProductPrice(String products){ double totalPrice = 0.0; StringBuilder priceDetails = new StringBuilder(); String[] singleProduct = products.split("; "); for(int i = 0; i < singleProduct.length; i++){ String[] productInfo = singleProduct[i].split(", "); totalPrice += Double.parseDouble(productInfo[2]); priceDetails.append(productInfo[2]); if(i < singleProduct.length - 1){ priceDetails.append(" + "); } } System.out.println(priceDetails + " = " + totalPrice); } public static void main(String arg[]){ Main obj = new Main(); obj.getProductPrice("1, dragonfruit, 12.50; 2, guava, 23.45; 3, avocado, 395.67"); } }
輸出:
12.50 + 23.45 + 395.67 = 431.62
Java 中的介面允許使用者跨不同類別呼叫相同的方法,每個類別都實作自己的邏輯 [7]。在下面的範例中,方法calculatePrice()在不同的類別中調用,例如Fruit和DiscountFruit,每個類別應用自己獨特的計算邏輯。
範例:
public String trim()
public class Main{ public static void main(String args[]){ String str = " You can do it! "; System.out.println(str); System.out.println(str.trim()); } }
You can do it! You can do it!
public static boolean isDigit(char ch)
輸出:
public class Main{ // return true when the given parameter has a digit public boolean searchDigit(String str){ for(int i = 0; i < str.length(); i++){ // charAt() method returns the character at the specified index in a string if(Character.isDigit(str.charAt(i))){ return true; } } return false; } // print digit index and value public void digitInfo(String str){ for(int i = 0; i < str.length(); i++){ if(Character.isDigit(str.charAt(i))){ System.out.println("Digit: " + str.charAt(i) + " found at index " + i); } } } public static void main(String args[]){ Main obj = new Main(); String[] strList = {"RT7J", "1EOW", "WBJK"}; for(String str : strList){ if(obj.searchDigit(str)){ obj.digitInfo(str); }else{ System.out.println("No digit"); } } } }
[1] JavaRush,java 中的 split 方法:將字串拆分為多個部分,2023 年 8 月 8 日
[2] W3Schools,Java String trim() 方法
[3] GeeksforGeeks,Java 中的 Character isDigit() 方法及其範例,2020 年 5 月 17 日
[4] 教學點,Java - 字元 isLetter() 方法
[5] Java 範例中的 DigitalOcean、Comparable 和 Comparator,2022 年 8 月 4 日
[6] Shiksha,了解 Java 中的使用者定義異常,2024 年 4 月 25 日
[7] Scientech Easy,Java 中的介面使用範例,2024 年 7 月 9 日
以上是Java學習之旅的詳細內容。更多資訊請關注PHP中文網其他相關文章!