活跃农民
- 积分
- 350
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-5-3
- 最后登录
- 1970-1-1
|
本帖最后由 James822 于 2016-5-20 02:25 编辑
作业虽然完成了 但我和其他同学不太一样 并不是卡在并补的计算上 而是对throw catch感觉没搞懂
一直在想为什么不直接判mylist是否为Null来表示是正常节点还是不可用节点(sentinel或者已删除的节点) 而必须要throw再catch
大家代码里应该都会用到这样的表达吧 比如对某个DList遍历的时候
那try 是加在while这个loop呢还是里面.item的函数外面呢
因为while里判断的条件正是throw判断的isvalid的代码所以感觉很重复 而且只要能进while循环的节点 只要不next prev去操作其他Node的话 肯定是合法的
但由于throw catch的语法又必须要求你写try 即使while循环里没有这样的操作(比如仅仅是对本node的item())
真心没搞懂为什么要加throw catch操作而不是直接利用Mylist的Null给外部访问的人
视频里老师说的是invalidenode的异常信息比null提供的信息更全 可我完全可以提供多个表示不同含义的invalid常量也能做到
比如null 是 0, invalid node是65535之类的
while (sIncur.isValidNode()) //总会先判这个节点的有效性
{
.....
sIncur.item(); //这中间又会用到Item的值
.....
}
运行的结果如下 感谢前面同学提供空集合测试代码- Set s = { 3 4 }
- Set s2 = { 4 5 }
- Set s3 = { 3 5 8 }
- After s.union(s2), s = { 3 4 5 }
- After s.intersect(s3), s = { 3 5 }
- s.cardinality() = 2
- Empty Set s4 = { }
- s4.cardinality() = 0
- After s4.union(s4), s4 = { }
- After s4.intersect(s4), s4 = { }
- After s3.union(s4), s3 = { 3 5 8 }
- After s3.intersect(s4), s3 = { }
复制代码- An empty list should be [ ]: [ ]
- l.isEmpty() should be true: true
- l.length() should be 0: 0
- Finding front node p of l.
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- Finding back node p of l.
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- l after insertFront(10) should be [ 10 ]: [ 10 ]
- l is a list of 3 elements: [ 1 2 3 ]
- n.item() should be 1: 1
- n.item() should be 2: 2
- n.item() should be 2: 2
- n.item() should be 4: 4
- n.item() should be 3: 3
- n.item() should be 6: 6
- After doubling all elements of l: [ 2 4 6 ]
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- n.item() should be 6: 6
- n.item() should be 12: 12
- n.item() should be 4: 4
- n.item() should be 8: 8
- n.item() should be 2: 2
- n.item() should be 4: 4
- After doubling all elements of l again: [ 4 8 12 ]
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- Removing middle element (8) of l: 8
- l is now: [ 4 12 ]
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- Removing end element (12) of l: 12
- l is now: [ 4 ]
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
- Removing first element (4) of l: 4
- l is now: [ ]
- p.isValidNode() should be false: false
- p.item() should throw an exception, and did.
- p.setItem() should throw an exception, and did.
- p.next() should throw an exception, and did.
- p.prev() should throw an exception, and did.
- p.insertBefore() should throw an exception, and did.
- p.insertAfter() should throw an exception, and did.
- p.remove() should throw an exception, and did.
复制代码 |
|