Arrays.sort(a) 自定义排序,(需实现接口:Comparable)

package com.hd;

import java.util.Arrays;

class Person implements Comparable{
int id ;
int score ;
public Person(int id,int score){
this.id = id;
this.score = score ;
}
@Override
public String toString(){
return "id:"+id+" score:"+score ;
} @Override
public int compareTo(Object o) {
Person p = (Person)o ;
int temp ;
     temp= this.score > p.score ? 1:this.score==p.score?0:-1;
if(temp==0)
return this.id >p.id? 1:-1;
return temp ;
} }
public class Main
{
public static void main(String args[])
{
Person p[]={new Person(9,80),
            new Person(8,90),
            new Person(8,70)};
Arrays.sort(p);
System.out.println(Arrays.toString(p)); }
}
//---------[id:8 score:70, id:9 score:80, id:8 score:90]

 1. sort():

Arrays.sort(Object[] a) --> Arrays.mergeSort()
--->((Comparable) dest[j-1]).compareTo(dest[j])>0
Comparable转换是为了调用compareTo()
      虽不在编译的时候报错,但如果没实现的话,
      运行时会:ClassCastException*,


 2. toString():

Arrays.toString(Object[] a)
          let the string of array append :[ .... ]
---->include:String.valueOf(Object a)
----->include:Object.toString()
--< self implement
        (overwrite the method of super class,Object)
          impelment person.toString();


 3. 导包:

     为什么在Arrays中可以未经导包,自由出现Comparable接口,
java.lang包是核心类库,它包含了运行java程序必不可少的系统类,
     系统自动为程序引入java.lang包中的类
    (如:System Math String)因此不需要再import引入;

-----------------------------------------------
   4.继承:

  (1)构造:
   子类的构造方法在创建一个子类的对象时,总是先调用父类的某个构造方法,
     如无指出,则是无参数的那个;
   因此,在子类创建对象时,不仅子类中声明的成员变量被分配了内存,
     而且父类中的成员变量也被分配了内存,
   看似浪费 ,但是注意,子类中还有一部分中的方法时从父类继承的
     这部分方法却可以操作这部分未继承的private成员;

 (2)重写类型
    jdk1.5后,允许重写方法的类型可以是父类方法的子类型,即不必完全一致,
     也就是说如果父类方法中的类型是类,则重写方法中可以是子类,

 (3)上转型
    对象的上转型对象:(前提是 B extends A )
    A a ;
    a = new B() ;
    但是上转型对象会失去原对象的一些属性,
    1,。不能操作子类新增的属性;
    2. 继承的或重写的,
    因为在Object 中没有实现,


 5.接口:
    不能自己new对象,
    只能得到所实现的类的引用,然后使用所实现的方法,接口回调;


Arrays.sort(a) 自定义排序的更多相关文章

  1. [LeetCode] Custom Sort String 自定义排序的字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  2. [leetcode]791. Custom Sort String自定义排序字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  3. 5.4 集合的排序(Java学习笔记)(Collections.sort(),及Arrays.sort()底层分析)

    1.Comparable接口 这个接口顾名思义就是用于排序的,如果要对某些对象进行排序,那么该对象所在的类必须实现 Comparabld接口.Comparable接口只有一个方法CompareTo() ...

  4. C# - List.Sort()自定义排序方法

    本文通过示例介绍了C#中典型容器List.Sort()的自定义排序方法,进而引出了C#中自定义排序的核心接口及方法 项目地址:自定义Sort方法 - SouthBegonia's Github Lis ...

  5. Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用

    摘要:本文主要介绍Java8 中Arrays.sort()及Collections.sort()中Lambda表达式及增强版Comparator的使用. 不废话直接上代码 import com.goo ...

  6. Arrays.sort解析

    Arrays.sort()解读 在学习了排序算法之后, 再来看看 Java 源码中的, Arrays.sort() 方法对于排序的实现. 都是对基本数据类型的排序实现, 下面来看看这段代码: Arra ...

  7. 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序

    默认升序 package peng; import java.util.Arrays;  public class Testexample { public static void main(Stri ...

  8. java.util.Arrays.sort两种方式的排序(及文件读写练习)

    import java.io.*; import java.util.*; public class SortTest{ public static void main(String args[]) ...

  9. 31、Arrays数组排序(续)——自定义排序

    自定义的类要按照一定的方式进行排序,比如一个Person类要按照年龄进行从小到大排序,比如一个Student类要按照成绩进行由高到低排序. 这里我们采用两种方式,一种是使用Comparable接口:让 ...

随机推荐

  1. no main manifest attribute, in demo-1.0.jar

    今天想打包一个jar到Linux上运行,发现使用java -jar demo-1.0.jar 运行报错: no main manifest attribute, in demo-1.0.jar 解决方 ...

  2. 模型表单ModleForm

    官方文档网址   http://python.usyiyi.cn/documents/django_182/topics/forms/modelforms.html 模型表单的应用场景 如果你正在构建 ...

  3. 【BZOJ1101】Zap(莫比乌斯反演)

    题意:多组询问,对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. T,a,b,d,x,y<=50000 思路:下底函数分块+积性函数 ...

  4. 制作不随浏览器滚动的DIV-带关闭按钮

    制作不随浏览器滚动的DIV 效果见 http://bbs.csdn.net/topics/90292438  的滚动效果. $(function(){ //获取要定位元素距离浏览器顶部的距离 var ...

  5. Day 20 迭代器、生成器

    一. 迭代器 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代对象:实 ...

  6. Servlet 2.4 规范之第四篇:Servlet上下文

    SRV.3.1    ServletContext接口说明 ServletContext接口定义了运行servlet的web应用中和servlet相关的视图信息.容器提供者负责提供ServletCon ...

  7. Python Challenge 第三关

    进入第三关,还是一张图加一句话:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. 图 ...

  8. npm 安装出现 UNMET DEPENDENCY 的解决方案

    npm imuzhicloud@0.2.2 E:\com\muzhicloud_make_web+-- UNMET PEER DEPENDENCY react@15.4.2+-- react-dnd@ ...

  9. est6 -- Object.is()、Object.assign()、Object.defineProperty()、Symbol、Proxy

    Object.is()用来比较两个值是否严格相等.它与严格比较运算符(===)的行为基本一致,不同之处只有两个:一是+0不等于-0,二是NaN等于自身. + === - //true NaN === ...

  10. 精读《Function Component 入门》

    1. 引言 如果你在使用 React 16,可以尝试 Function Component 风格,享受更大的灵活性.但在尝试之前,最好先阅读本文,对 Function Component 的思维模式有 ...