暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

数组排序

lvlvstart 2017-03-19
165

Arrays.sort 方法

1.Arrays 类中的sort方式提供对数组默认的升序操作。

2.Arrays  的排序是对快速排序算法的优化。

例:

int[] a = new int[]{13,10,9};

Arrays.sort(a);

System.out.println(Arrays.toString(a)); //输出 [9, 10, 13]

数组的倒叙

1.方式一

java提供基本数据类型所对应的数据包装类。例如int 类型对应Integer 对象

Integer[] halouworld = new Integer[]{123,32342 ,4};

Arrays.sort(halouworld);

System.out.println(Arrays.toString(halouworld));

List<Integer> list = Arrays.asList(halouworld); //数组的倒叙

Collections.reverse(list);

System.out.println(Arrays.toString(list.toArray()));

2.方式二

Integer[] halouworld = new Integer[]{123,32342 ,4 ,1931239};

Arrays.sort(halouworld);

int circleTimes = halouworld.length/2;

for(int i = 0 ; i <circleTimes ; i ++){

    int temp = halouworld[i];

    halouworld[i] = halouworld[halouworld.length -1-i];

    halouworld[halouworld.length - 1 - i] = temp;

}

System.out.println(Arrays.toString(halouworld));


文章转载自lvlvstart,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论