【java基礎學習一】int[]、Integer[]、String[] 排序( 正序、倒敘)、去重


調用:
//重復項有9、5、1、2
        int[] ints = new int[]{9,4,7,8,2,5,1,6,2,5,9,1};
        arrayIntTest(ints);
        /////////////////////////////
        //重復項有9、5、1、2
        Integer[] integers = new Integer[]{9,4,7,8,2,5,1,6,2,5,9,1};
        arrayIntegerTest(integers);
        /////////////////////////////
        //重復項有e、g
        String[] strs = new String[]{"e","t","a","d","g","c","A","f","e","g","Q","h"};
        arrayStringTest(strs);

 

 /**
     * int[]數組操作 正序、倒敘、去重
     * @param arr
     */
    public static void arrayIntTest(int[] arr) {
        int length = arr.length;

        //int[]正序
        Arrays.sort(arr);
        //int[]倒序
        Arrays.sort(arr);
        ArrayUtils.reverse(arr);
        System.out.print("");

        //int[]正序
        int[] arr1 = Arrays.stream(arr).boxed().sorted().mapToInt(p -> p).toArray();
        System.out.print("");
        //int[]倒序
        int[] arr2 = Arrays.stream(arr).boxed().sorted((s1, s2) -> {return s2 > s1 ? 1 : -1;}).mapToInt(p -> p).toArray();
        System.out.print("");
        //int[]去重
        int[] arr3 = Arrays.stream(arr).boxed().distinct().mapToInt(p -> p).toArray();

    }

    /**
     * Integer[]數組操作 正序、倒敘、去重
     * @param arr
     */
    public static void arrayIntegerTest(Integer[] arr){
        int length = arr.length;

        //Integer[]正序
        Arrays.sort(arr);
        //Integer[]倒序
        Arrays.sort(arr, Collections.reverseOrder());
        //Integer[]倒序
        Arrays.sort(arr);
        ArrayUtils.reverse(arr);

        //Integer[]去重
        Set<Integer> set = new HashSet<Integer>();
        set.addAll(Arrays.asList(arr));
        Integer[] arr4 = new Integer[set.size()];
        set.toArray(arr4);

        //Integer[]正序,去重
        Set set1=new TreeSet(Arrays.asList(arr));
        Integer[] arr5 = new Integer[set1.size()];
        set1.toArray(arr5);

        //Integer[]正序
        Integer[] arr1 = new Integer[arr.length];
        Arrays.stream(arr).sorted().collect(Collectors.toList()).toArray(arr1);
        //Integer[]倒序
        Integer[] arr2 = new Integer[arr.length];
        Arrays.stream(arr).sorted((s1, s2) -> {return s2>s1?1:-1;}).collect(Collectors.toList()).toArray(arr2);

        //Integer[]去重
        List<Integer> list1 =  Arrays.stream(arr).distinct().collect(Collectors.toList());
        Integer[] arr3 = new Integer[list1.size()];
        list1.toArray(arr3);
    }
    /**
     * String[] 操作 正序、倒敘、去重
     * @param arr
     */
    public static void arrayStringTest(String[] arr){
        int length = arr.length;

        //String[]正序
        Arrays.sort(arr);
        //String[]倒序
        Arrays.sort(arr, Collections.reverseOrder());

        //String[]正序 不區分大小寫
        Arrays.sort(arr, String.CASE_INSENSITIVE_ORDER);
        //String[]倒序 不區分大小寫
        Arrays.sort(arr, String.CASE_INSENSITIVE_ORDER);
        Collections.reverse(Arrays.asList(arr));

        //String[]去重
        Set<String> set = new HashSet<String>();
        set.addAll(Arrays.asList(arr));
        String[] arr4 = new String[set.size()];
        set.toArray(arr4);

        //String[]正序,去重
        Set set1=new TreeSet(Arrays.asList(arr));
        String[] arr5 = new String[set1.size()];
        set1.toArray(arr5);

        //String[]去重
        List<String> list1 =  Arrays.stream(arr).distinct().collect(Collectors.toList());
        String[] arr1 = new String[list1.size()];
        list1.toArray(arr1);
    }

此代碼只是練習,有問題大家隨時溝通此片練習,有諸多累贅,請大家選擇合適的運用。

從今日起,由於形勢所迫轉java,java隨筆今日正式開寫,.net暫時告一段落。說多了都是淚。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM