Rumah >Java >javaTutorial >Kembara Belajar Java
Saya baru-baru ini mempelajari Java melalui amalan dalam [https://exercism.org/tracks/java/exercises]. Kemajuan semasa saya ialah 13 daripada keseluruhan 148 amalan. Saya ingin berkongsi apa yang saya pelajari.
Siaran ini memperkenalkan pemahaman saya tentang .split(), .trim(), .isDigit(), .isLetter(), Comparable
Definisi: Kaedah .split() membahagikan String kepada tatasusunan berdasarkan pemisah [1].
Sintaks:
public String[] split(String regex, int limit)
Parameter:
Contoh:
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"); } }
Output:
12.50 + 23.45 + 395.67 = 431.62
Definisi: Kaedah .trim() mengalih keluar ruang putih daripada kedua-dua hujung rentetan [2].
Sintaks:
public String trim()
Parameter:
Contoh:
public class Main{ public static void main(String args[]){ String str = " You can do it! "; System.out.println(str); System.out.println(str.trim()); } }
Output:
You can do it! You can do it!
Definisi: Kaedah .isDigit() menentukan sama ada aksara ialah digit atau bukan [3].
Sintaks:
public static boolean isDigit(char ch)
Parameter:
Contoh:
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"); } } } }
Output:
Digit: 7 found at index 2 Digit: 1 found at index 0 No digit
Definisi: Kaedah .isLetter() menentukan sama ada aksara itu huruf atau bukan [4].
Sintaks:
public static boolean isLetter(char ch)
Parameter:
Contoh:
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); } } }
Output:
A0178967547 has letter 'A' at index 0 0126H54786K5 has letter 'H' at index 4 0126H54786K5 has letter 'K' at index 10 0165643484 is valid
Definisi: Yang Setanding
Contoh:
// 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)); } }
Output:
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]]
Definisi: Pengecualian Java yang ditakrifkan pengguna ialah pengecualian tersuai yang dibuat oleh pembangun untuk mengendalikan keadaan ralat tertentu [6].
Contoh:
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"); } }
Output:
12.50 + 23.45 + 395.67 = 431.62
Antara muka dalam Java membenarkan pengguna menggunakan kaedah yang sama merentas pelbagai kelas, masing-masing melaksanakan logiknya sendiri [7]. Dalam contoh di bawah, kaedah calculatePrice() dipanggil dalam kelas yang berbeza, seperti Fruit dan DiscountFruit, dengan setiap kelas menggunakan logik pengiraan uniknya sendiri.
Contoh:
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)
Output:
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, kaedah belah dalam java: belah rentetan kepada bahagian, 8 Ogos 2023
[2] Kaedah W3Schools, Java String trim()
[3] Kaedah GeeksforGeeks, Character isDigit() dalam Java dengan contoh, 17 Mei, 2020
[4] tutorialspoint, Java - Kaedah Character isLetter()
[5] DigitalOcean, Comparable and Comparator in Java Contoh, 4 Ogos 2022
[6] Shiksha, Memahami Pengecualian Ditentukan Pengguna dalam Java, 25 Apr 2024
[7] Scientech Easy, Penggunaan Antara Muka dalam Java dengan Contoh, 9 Julai 2024
Atas ialah kandungan terperinci Kembara Belajar Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!