CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

关于接口的问题---基础问题~~~~~~`

楼主bigdog942(zhiyuanhu)2005-08-03 12:29:34 在 Java / J2SE / 基础类 提问

我看java核心技术时有些地方想不通  
  就是  
  Array要实现对employee对象数组排序  
  就是用sort排序  
  staff[0]=new   employee("harry   hacker",35000);  
  staff[1]=new   employee("car1   cracker",75000);  
  staff[2]=new   employee("tony   tester",38000);  
   
  Arrays.sort(staff);  
   
  结果应该是什么呢?是按字母排序还是按后面的工资排序?为什么可以实现这种排序呢?  
  实现这个有什么用呢?书上说得不太清楚请大家帮我解释一下,实现下面这个方法就能进行排序?  
  class   employee   implements   Comparable  
  {  
      ..............  
      public   int   comparaTO(Object   otherObject)  
    {  
      employee   other=(employee)otherObject;  
      if(salary<other.salary)   return   -1;  
      if   (salary>other.salary)   return   11;  
    return   0;  
    }  
    ................  
  } 问题点数:30、回复次数:6Top

1 楼star_str(生命火花)回复于 2005-08-03 12:37:06 得分 0

Comparable这个接口是怎么定义的啊Top

2 楼nostalgica(黄s)回复于 2005-08-03 12:37:38 得分 30

sort实际是调用comparaTO进行比较,当然要实现Comparable接口了,很明显是通过工资排序。Top

3 楼nostalgica(黄s)回复于 2005-08-03 12:38:53 得分 0

java.lang.Comparable  
  不用自已定义。Top

4 楼losingaries()回复于 2005-08-03 16:27:50 得分 0

支持楼上的Top

5 楼interhanchi(on the Java Road)回复于 2005-08-03 16:34:19 得分 0

sort  
  public   static   void   sort(Object[]   a)  
   
  Sorts   the   specified   array   of   objects   into   ascending   order,   according   to   the   natural   ordering   of   its   elements.   All   elements   in   the   array   must   implement   the   Comparable   interface.   Furthermore,   all   elements   in   the   array   must   be   mutually   comparable   (that   is,   e1.compareTo(e2)   must   not   throw   a   ClassCastException   for   any   elements   e1   and   e2   in   the   array).  
  This   sort   is   guaranteed   to   be   stable:   equal   elements   will   not   be   reordered   as   a   result   of   the   sort.  
   
  The   sorting   algorithm   is   a   modified   mergesort   (in   which   the   merge   is   omitted   if   the   highest   element   in   the   low   sublist   is   less   than   the   lowest   element   in   the   high   sublist).   This   algorithm   offers   guaranteed   n*log(n)   performance.    
   
  那要看你的compareTo怎么定义的了!   想用那个排都可以!  
   
  要多翻一下jdk文档!   你所问的文档里面都有!  
   
   
  Top

6 楼jhzhao2002(debug)回复于 2005-08-03 16:41:51 得分 0

Arrays.sort()这是个函数中调用comparaTO函数判断大小。然后排序。Top

相关问题

  • JAVA基础之类、接口
  • 一个接口的基础问题
  • 一个很基础的问题,什么接口才是双接口,是不是从IDispatch派生的接口就是双接口???
  • 接口
  • 接口
  • 一个基础问题:一般所说的“给其他系统预留的接口”是什么?
  • DirectDraw的接口
  • 关于接口...
  • 接口调度
  • 接口问题。

关键词

  • 排序
  • 接口
  • comparato
  • sort
  • staff
  • employee
  • comparable
  • 实现
  • arrays
  • element

得分解答快速导航

  • 帖主:bigdog942
  • nostalgica

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo