专业知识-算法

1 关于Java的sort()函数,为何能根据一个参数——即数组名,进行排序?

how can sort, now, how to compare data of all those different types without being given any information about the type of an item's key? And the answer is that what is we set up a mechanism known as a callback or reference to executable code where the client, by passing an array of objects to the sort function. In Java, there's an implicit mechanism that says that any such array of object is going to have the compareTo() method, then the sort function calls back the compareTo() method associated with the objects in the array when it ever needs, whenever it needs to compare two items. There's a lot of different ways to implement callbacks and 


Now, built in to Java is the so-called the Comparable interface and all the Comparable interface is the specification that a type, data type that implements Comparable will have a compareTo() method


2 Java File类

构造方法:

File(File parent, String child) 
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
 
File(String pathname) 
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
 
File(String parent, String child) 
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
 
File(URI uri) 
通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。

方法:

compareTo(File pathname) 按字母顺序比较两个抽象路径名。 

listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件  


3 Java implements

引用:http://blog.csdn.net/sunnytina/article/details/6448408

“mplements 也是实现父类和子类之间继承关系的关键字,如类 A 继承 类 B 写成 class A implements B{}.


    这是百度百科上的解释:


      implements是一个类实现一个接口用的 关键字 ,他是用来实现接口中定义的抽象方法。比如:people是一个接口,他里面有say这个方法。public interface people(){ public say();}但是接口没有方法体。只能通过一个具体的类去实现其中的方法体。比如chinese这个类,就实现了people这个接口。 public class chinese implements people{ public say() {System.out.println("你好!");}}


    与Extends的不同


  extends, 可以实现父类,也可以调用父类初始化 this.parent()。而且会覆盖父类定义的变量或者函数。这样的好处是:架构师定义好接口,让工程师实现就可以了。整个项目开发效率和开发成本大大降低。


  implements,实现父类,子类不可以覆盖父类的方法或者变量。即使子类定义与父类相同的变量或者函数,也会被父类取代掉。


  这两种实现的具体使用,是要看项目的实际情况,需要实现,不可以修改implements,只定义接口需要具体实现,或者可以被修改扩展性好,用extends。


   


     记得张老师的视频中这样讲到:若同时用到 extends 和 implements 的时候,extends 必须放在 implements 关键字之前。如 : class A extends B implements C.


      以后学习中有更深的理解会不断完善的。”


引用知乎答案:

"作者:Dion
链接:https://www.zhihu.com/question/20111251/answer/14012223
来源:知乎
著作权归作者所有,转载请联系作者获得授权。


“接口是个规范”,这句没错。
“不如直接就在这个类中写实现方法岂不是更便捷”,你怎么保证这个接口就一个类去实现呢?如果多个类去实现同一个接口,程序怎么知道他们是有关联的呢?


既然不是一个类去实现,那就是有很多地方有用到,大家需要统一标准。甚至有的编程语言(Object-C)已经不把接口叫 interface,直接叫 protocol。


统一标准的目的,是大家都知道这个是做什么的,但是具体不用知道具体怎么做。
比如说:
我知道 Comparable 这个接口是用来比较两个对象的,那么如何去比较呢?
数字有数字的比较方法,字符串有字符串的比较方法,学生(自己定义的类)也有自己的比较方法。


然后,在另外一个负责对象排序(不一定是数字喔)的代码里面,肯定需要将两个对象比较。
这两个对象是什么类型呢?
Object a,b?肯定不行,a > b 这样的语法无法通过编译。
int a,b?也不行?一开始就说了,不一定是数字。
....
所以,Comparable 就来了。他告诉编译器,a b 两个对象都满足 Comparable 接口,也就是他们是可以进行比较的。具体怎么比较,这段程序不需要知道。
所以,他需要一些具体的实现,Comparable 接口有一个方法,叫 compareTo。那么这个方法就是用来取代 <、> 这样的运算符。
因为运算符是编译器保留给内置类型(整数、浮点数)进行比较用的,而不是一个广义的比较运算。


如果你可以明白 JDK 自身库里面诸如 Comparable 这样已经有的接口,那么就很容易理解自己在开发程序的时候为什么需要用到接口"


接口中的方法没有方法体,相当于一个口号,在类中具体定义方法体,好处是可以把属于不同群体的工作分离开来,仿佛两个对接的轴:没有接口时,一根轴尺寸变了,另一根也得便;有接口后,只需要变一根轴就ok了


sort()函数带的参数是一个comparable的数组名,做比较是应满足三条法则:传递性、反自反和totalit,即v<或v>w或v=w


4 Shellsort

"if you've got an array that's h-sorted and then you k-sort it for another value k different from h, it's still h-sorted. This is one of those mathematical facts that seems obvious but then if you try to prove that maybe it's a little more subtle than you think. So, if you think of all this is, is, is trivial and easy, go ahead and try to write down a proof that a g-sorted array remains g-sorted even after it's h-sorted. But most people will accept that and it's a fact and that's how "



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值