我最近通过[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中文网其他相关文章!