请选择 进入手机版 | 继续访问电脑版
楼主: Nicolle
3263 13

[读者文库]Java® Programming Interviews Exposed Wiley 2014 [推广有奖]

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python(Must-Read Books)

SAS Programming

Must-Read Books

威望
16
论坛币
12402323 个
通用积分
1620.8615
学术水平
3305 点
热心指数
3329 点
信用等级
3095 点
经验
477211 点
帖子
23879
精华
91
在线时间
9878 小时
注册时间
2005-4-23
最后登录
2022-3-6

Nicolle 学生认证  发表于 2014-7-24 06:49:31 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

本帖被以下文库推荐

cylqd 发表于 2014-7-24 07:05:56 |显示全部楼层 |坛友微信交流群
感谢楼主肺腑之言

使用道具

cylqd 发表于 2014-7-24 07:07:24 |显示全部楼层 |坛友微信交流群

使用道具

songlinjl 发表于 2014-7-24 07:34:52 |显示全部楼层 |坛友微信交流群
感谢你的辛劳

使用道具

liubingzsd 发表于 2014-7-24 21:49:59 |显示全部楼层 |坛友微信交流群
学习了,谢谢

使用道具

文津国际 发表于 2014-7-25 02:33:48 |显示全部楼层 |坛友微信交流群
thanks

使用道具

fhc2010 发表于 2014-7-31 00:20:11 |显示全部楼层 |坛友微信交流群
want to look

使用道具

nonewman 发表于 2014-7-31 07:53:42 |显示全部楼层 |坛友微信交流群
a glance for interview

使用道具

ReneeBK 发表于 2015-6-6 08:58:10 |显示全部楼层 |坛友微信交流群
  1. Listing 4-1: Naturally sorting an array of ints

  2. @Test
  3. public void sortInts() {
  4.     final int[] numbers = {-3, -5, 1, 7, 4, -2};
  5.     final int[] expected = {-5, -3, -2, 1, 4, 7};

  6.     Arrays.sort(numbers);
  7.     assertArrayEquals(expected, numbers);
  8. }
  9. For an array of Objects, the type being sorted must implement the Comparable interface, as shown in Listing 4-2.

  10. Listing 4-2: Sorting objects naturally

  11. @Test
  12. public void sortObjects() {
  13.     final String[] strings = {"z", "x", "y", "abc", "zzz", "zazzy"};
  14.     final String[] expected = {"abc", "x", "y", "z", "zazzy", "zzz"};

  15.     Arrays.sort(strings);
  16.     assertArrayEquals(expected, strings);
  17. }
  18. The String class implements the Comparable interface, so the sorting works as you would expect. If the type being sorted does not implement Comparable, this will throw a ClassCastException.

  19. For your own class definitions, you will need to implement Comparable, such as the implementation described in Listing 4-3.

  20. Listing 4-3: Sorting without a Comparable interface

  21. private static class NotComparable {
  22.     private int i;
  23.     private NotComparable(final int i) {
  24.         this.i = i;
  25.     }
  26. }

  27. @Test
  28. public void sortNotComparable() {
  29.     final List<NotComparable> objects = new ArrayList<>();
  30.     for (int i = 0; i < 10; i++) {
  31.         objects.add(new NotComparable(i));
  32.     }

  33.     try {
  34.         Arrays.sort(objects.toArray());
  35.     } catch (Exception e) {
  36.         // correct behavior – cannot sort
  37.         return;
  38.     }

  39.     fail();
  40. }
复制代码


It is not possible to use the Collections.sort method because the compiler expects the type of the parameter to be an implementation of Comparable. The method signature is:

public static <T extends Comparable<? super T>> void sort(List<T> list)
If you want to provide your own ordering, you provide an implementation of the Comparator interface to the sort method. This interface has two methods: int compare(T o1, T o2) for the implementing type T, and boolean equals(Object o). The compare method returns an int in one of three states: negative if the first argument is sorted before the second, zero if the two are equal, or positive if the second argument is sorted first.

使用道具

ReneeBK 发表于 2015-6-6 08:59:07 |显示全部楼层 |坛友微信交流群
  1. Listing 4-4: A reverse numerical order Comparator

  2. public class ReverseNumericalOrder implements Comparator<Integer> {
  3.     @Override
  4.     public int compare(Integer o1, Integer o2) {
  5.         return o2 - o1;
  6.     }
  7.     // equals omitted
  8. }
  9. Listing 4-5 uses this Comparator.

  10. Listing 4-5: Using a custom ordering

  11. @Test
  12. public void customSorting() {
  13.     final List<Integer> numbers = Arrays.asList(4, 7, 1, 6, 3, 5, 4);
  14.     final List<Integer> expected = Arrays.asList(7, 6, 5, 4, 4, 3, 1);

  15.     Collections.sort(numbers, new ReverseNumericalOrder());
  16.     assertEquals(expected, numbers);
  17. }
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-18 14:51