摘要: 从设计目的上来说,接口和抽象类,本身就具有者非常类似的目的。那就是都是想制定一套规则,将具体问题进行抽象,归纳。但接口与抽象类还是有一定区别:1)二者不在一个层级 抽象类可以实现接口,但接口却无法继承抽象类吧?所以严格来说,接口是抽象类的上级。2)作用域不同 接口体现了的是相同类别或者不同类别下事物的归纳,而抽象类则更多的体现在对相同类别事务的归纳上。这在编程中的体现就是一个类可以实现多个接口,而不能继承多个抽象类,因为一个人能有各种的功能,这些功能可能是人类特有的,也可能是和其他动物共有的,但绝对不能有一个以上的亲生父亲。3)开放性有所不同 接口和抽象类虽然相似,但在某些场合,如分布式... 阅读全文
posted @ 2013-09-02 17:26 infinityu 阅读(229) 评论(0) 推荐(0) 编辑
摘要: 装饰器模式 vs 适配器模式 vs 代理模式 共通点:都是对被依赖类/被依赖对象的封装 意图方面:装饰模式的重点在于“扩展”:动态地为一个类添加额外的职责;适配器和代理模式体现的是封装:适配器的封装重点在于“隐藏”内部变化(对适配器来说也是伪装),是系统/组件升级良药,使得新旧系统兼容;代理模式定义是为其他对象提供一种代理以控制对这个对象的访问,重点在于“控制”。 实现方面:装饰器模式发生在运行时,属于动态扩展,使用时将被装饰对象作为参数传递给装饰器的构造函数,客户知道被装饰的对象;适配器模式和代理模式均在编译期实现。被适配和被代理的对象对于客户透明;适配器模式 vs 外观模式 共通点... 阅读全文
posted @ 2013-08-26 09:35 infinityu 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 继承于HashMap,定义了新的内部类Entry用于实现双向链表保存记录的插入或访问顺序;accessOrder用于指示链表保存记录采用的顺序,true为访问顺序,false为插入顺序;加入新的记录时需要更新链表,访问记录时需要更新链表(更具accessOrder值判断是否实际更新);removeEldestEntry方法在LinkedHashMap用于表示缓存时有用,通过重载该方法(修改更新链表的逻辑以及返回值),删除有限容量下缓存中的最旧记录; 1 public class MyLinkedHashMap extends MyHashMap { 2 private boolean ... 阅读全文
posted @ 2013-08-15 12:37 infinityu 阅读(301) 评论(0) 推荐(0) 编辑
摘要: Entry类中需包含键值的hash值,防止resize时的重复计算;Map容量为2的整幂,可使用位操作取代取余操作提高效率;resize时需要将原table的桶内数据置null,利于垃圾回收;hash函数考虑数据高位的影响,可减少冲突。 1 public class MyHashMap implements IMap { 2 static final float DEFAULT_LOAD_FACTOR = 0.75f; 3 static final int DEFAULT_CAPACITY = 16; 4 5 Entry[] table; 6 in... 阅读全文
posted @ 2013-08-14 15:56 infinityu 阅读(231) 评论(0) 推荐(0) 编辑
摘要: 更新测试http://blog.csdn.net/lifetragedy/article/details/9718567 阅读全文
posted @ 2013-07-30 14:51 infinityu 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 由一个问题引出:Java到底是传引用还是传值?如果是传引用,那么为何badSwap函数并没有如预想的那样交换变量? 1 public void badSwap(int var1, int var2) 2 3 { 4 5 int temp = var1; 6 7 var1 = var2; 8 9 var2 = temp;10 11 }如果是传值,那么为何tricky函数能够改变外部变量的值? 1 public void tricky(Point arg1, Point arg2) 2 3 { 4 5 arg1.x = 100; 6 7 arg... 阅读全文
posted @ 2013-06-20 11:03 infinityu 阅读(304) 评论(2) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2013-05-14 14:41 infinityu 阅读(164) 评论(0) 推荐(0) 编辑
摘要: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8/** * Definition for 阅读全文
posted @ 2013-05-14 14:18 infinityu 阅读(333) 评论(0) 推荐(0) 编辑
摘要: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 1 class Solution { 2 public: 3 int reverse(int x) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int sig = 1; 7 if(x<0){ 8 si... 阅读全文
posted @ 2013-05-14 12:43 infinityu 阅读(299) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass.解法 阅读全文
posted @ 2013-05-14 12:18 infinityu 阅读(308) 评论(0) 推荐(0) 编辑