看板java
标 题Re: [问题] compile後出现的note
发信站KKCITY (Wed Jul 19 11:28:42 2006)
转信站ptt!ctu-reader!Spring!news.nctu!news.ntu!bbs.ee.ntu!news.kkcity.com.tw
※ 引述《[email protected] (几百年没换昵称)》之铭言:
> ==> [email protected] ( ) 提到:
> 别这麽说
> 我不会想随便战人
> 我提到vector arraylist可以互换
> 是想要解释为啥我觉得Date有需要
> 就我个人经验而言
> date timestamp都有需要
> 就像我认为
> vector arraylist都有需要一样
> 我个人以为 可不可以被取代跟该不该消失是两回事
> 最後,当然可以互换
> 把arraylist synchronize起来就可以了(如下面的例子)
建议你找找网上的资料.
其实自 Collection 出现後, synchronized list 早该转
用 synchronized wrapper 来 wrap 着 List implementations.
Vector 和 Hashtable 还留着主要是为了 backward compatibility
> > 其实 Vector 是该被拔掉的...
> > 因为 Sun 现在建议是用 Collections.synchronizedList(new ArrayList())
> > 来达成 synchronized list
> 我只有在arraylist那边看到SUN建议当使用arraylist而且需要注意synchronization
> 时候这样用,
> 并没有看到SUN建议不要用vector
> http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html
> 这篇文章是我看到的对两者的一些说明
> > Alien
> 其实我都是看需求
> 有些时候用arraylist因为只会读,不会写
> 有synchronization考量的时候,再用vector
> 会这样是因为arraylist没有synchrinization机制,速度比较快
> 此外,在现在许多n-tier应用上,用interface来传递物件会比较自由
> 从callee回传到caller传的是interface
面对 interface 来工作是一回事.
Vector 有没有用又是另一回事了.
要 synchronized ArrayList, 我上面就给了
方法. Vector 倒是除了 for backward compatibility 没
有什麽价值了. 用 Vector 反而会令修改变得麻烦:
1) 不慎用了 legacy 的 Vector methods,
到时转用别的 List implementation 就不行
2) 就算是为了 synchronization, 用 syncrhonized
List wrapper 也能让你更简单转用别的 List implementation.
或者唯一的好处是 Vector 比 用wrapper 少了一层
method invocation. 但这种好处有多大呢?
> callee
> public Collection funcA()
> {
> Vector v....
> ...
> return v;
> }
> 而caller得到回传物件後,自己再决定如何使用
> public void funcB()
> {
> Collection c = funcA();
> 这里就随便你怎麽使用这个c
> 你可以转成Vector,也可以变成ArrayList
要转成 Vector 或 ArrayList 就代表你用错了
> }
> 这样两边的programmer都有比较多的弹性
明显你的例子是 error-prone 而不是 flexible :P
alien
--
┌─────◆KKCITY◆─────┐KKMAN团队 全新力作 ◎◎KKBOX◎◎
│ bbs.kkcity.com.tw │知名歌手通通都有 所有新歌想听就听
└──《From:202.72.0.45
》──┘※※ 内容丰富多元的线上音乐台 ※※
--