排序是编程中非常重要的功能,因为它按照所需的顺序设置数组的不同元素。通常,人们使用字母顺序或递增或递减顺序。排序通常是将原始且无组织形式的数据转换为正确且有组织的顺序,使其对人脑有意义。有两种方法可以对字符串数组进行排序。第一种方法称为自定义排序或用户定义逻辑排序,第二种方法称为自然排序或 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中文网其他相关文章!