调用:
//重复项有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暂时告一段落。说多了都是泪。。。

 

posted on 2016-10-12 16:31  秦岭过客  阅读(5388)  评论(0编辑  收藏  举报