排序是程式設計中非常重要的功能,因為它按照所需的順序設定陣列的不同元素。通常,人們會使用字母順序或遞增或遞減順序。排序通常是將原始且無組織形式的資料轉換為正確且有組織的順序,使其對人腦有意義。有兩種方法可以對字串陣列進行排序。第一種方法稱為自訂排序或使用者定義邏輯排序,第二種方法稱為自然排序或 Array.sort() 排序。在本文中,這兩種方法都使用多個範例及其工作原理進行了解釋。在本主題中,我們將學習 Java 中的字串陣列排序。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
以下是下面提到的例子和方法
可以透過將每個元素與其餘元素進行單獨比較來完成排序。在第一個範例中,執行相同的處理。使用了兩個循環,內循環避免了執行比較時的重複。當條件 (favouritefood[a].compareTo(favouritefood[b])>0) 結果大於 0 時,程式執行交換並完成陣列排序。類似地,使用條件檢查數組的所有元素並完成排序。在此範例中,元素使用使用者定義的邏輯或自訂排序來按字母順序排序。
import java.util.Arrays; public class Sortingexample1 { public static void main( String args[]) { String[] favouritefood = {" Pizza \n " , " Pasta \n " , " Chole Bhature \n " , " Paratha \n " , " Chowmein \n " , " Momo \n " , " Fried Rice \n " , " Maggie \n " , " Garlic Bread \n " , " Biryani \n " , " Gulab Jamun \n " }; System.out.print(" Rahul favourite foods are: \n " ); int length = favouritefood.length; for(int a = 0 ; a<length-1 ; a++) { for (int b = a+1 ; b<favouritefood.length ; b++) { if(favouritefood[a].compareTo( favouritefood[b] )>0) { String virtual = favouritefood[a]; favouritefood[a] = favouritefood[b]; favouritefood[b] = virtual; } } } System.out.println( Arrays.toString( favouritefood )); } }
輸出:
此範例是透過三個簡單步驟執行的。首先,使用循環我們必須將輸入字串轉換為字元陣列。現在,Arrays.sort(array1, new Comparator
import java.util.Arrays; import java.util.Comparator; public class Sortingexample2 { public static String sortString( String initialstringused ) { System.out.println( " Mixed kinda character string used \n " ); Character array1[] = new Character[ initialstringused.length() ]; for (int a = 0 ; a < initialstringused.length() ; a++) { array1[a] = initialstringused.charAt(a); } System.out.println( " We use character string here \n " ); Arrays.sort( array1, new Comparator<Character>() { @Override public int compare(Character FirstInput, Character SecondInput) { return Character.compare(Character.toUpperCase(FirstInput), Character.toUpperCase(SecondInput)); } }); StringBuilder b = new StringBuilder(array1.length); for ( Character c : array1 ) b.append( c.charValue() ); return b.toString(); } public static void main(String[] args) { String initialstringused = " \n Biryani is loved by Rahul \n "; String stringwegetinreturn = sortString( initialstringused ); System.out.println( " Got an output as : \n " + stringwegetinreturn ); System.out.println( " \n Initial sentence entered was : \n " + initialstringused ); } }
輸出:
自然排序分三步驟完成。首先,toCharArray()方法應用於輸入字串以建立輸入字串的字元陣列。現在,使用 Arrays.sort(char c[]) 方法對字元陣列進行排序。現在,字串類別建構子用於從字元陣列建立排序字串。
import java.util.Arrays; public class Sortingexample3 { public static String sortString( String initialstringused) { char sameArray[] = initialstringused.toCharArray(); System.out.println( " Mixed kinda character string used \n " ); Arrays.sort( sameArray); System.out.println( " We use character string here \n " ); return new String(sameArray); } public static void main(String[] args) { String initialstringused = " \n Choley Bhature is loved by Rahul \n "; String stringwegetinreturn = sortString( initialstringused ); System.out.println( "Input String : " + initialstringused ); System.out.println("Output String : " + stringwegetinreturn ); } }
輸出:
import java.util.Arrays; public class Sortingexample4 { public static void main(String args[]) { String[] favouritefood = {" \n Pizza \n " , " \n Pasta \n " , " \n Chole Bhature \n " , " \n Paratha \n " , " \n Chowmein \n " , " \n Momo \n " , " \n Fried Rice \n " , " \n Maggie \n " , " \n Garlic Bread \n " , " \n Biryani \n " , " \n Gulab Jamun \n " }; System.out.print(" \n Actual food list entered: \n " ); System.out.println(Arrays.toString(favouritefood)); System.out.print(" \n Rahul favourite foods are: \n " ); Arrays.sort(favouritefood); System.out.println(Arrays.toString(favouritefood)); } }
輸出:
在上面文章的基礎上,我們了解了Java中的排序字串陣列。對字串數組進行排序有兩種方法,本文中使用多個範例對這兩種方法進行了解釋。這些範例將幫助初學者了解 Java 程式碼中對字串陣列進行排序的實現,以獲得所需的結果。
以上是Java 中的字串陣列排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!