數組排序

Java 對數組進行排序輸出,代碼以下:java

區分大小寫使用 compareToIgnorecase
不區分大小寫使用 compareToapache

 
 
import java.util.Arrays;public class MainTest {    public static void main(String[] args) {        int[] intarr=new int[] {34,5,64,22,62,243,97,42,77};        System.out.println("排序前"+Arrays.toString(intarr));        Arrays.sort(intarr);        System.out.println("排序後"+Arrays.toString(intarr));        String[] strarr=new String[] {"java","App","nice","test","apache"};        System.out.println("\n排序前"+Arrays.toString(strarr));        for (int i=0;i<strarr.length-1;i++){            for (int j=0;j<strarr.length-i-1;j++) {                if(strarr[j].compareToIgnoreCase(strarr[j+1])>0){ //不區分大小寫使用compareToIgnoreCase  區分大小寫使用compareTo                    String temp=strarr[j];                    strarr[j]=strarr[j+1];                    strarr[j+1]=temp;                }            }        }        System.out.println("排序後"+Arrays.toString(strarr));    }}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息