由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Scala怎么通过index访问set或者array
相关主题
L家的高频题merge k sorted arrays giving iterators求讨论!hash_map 的遍历问题
reverse an array请教个面经里的设计题
问个题问个最近面试里的题目
iterator 实现 如何 peek(),pop()?Two problems from Google
问一道C++ template的面试题[google面试]iterator访问
请教 Iterator 一题面完G的电面了,忐忑
刷了半天题Google onsite归来
LC的BST iterator到底要考察什么?问道G 的题
相关话题的讨论汇总
话题: key话题: pos话题: iterator话题: def话题: array
进入JobHunting版参与讨论
1 (共1页)
a******3
发帖数: 113
1
定义了一个
val map = new ConcurrentSkipListMap[Key, Value]()
因为要实现多线程,需要重写他的iterator
我的代码如下
def keyIterator: Iterator[Key] = new Iterator[Key] {
val newMap=getMapClone
var set=newMap.keySet()
var pos = 0
def hasNext = pos < newMap.size()
def next: Key = { val z = array(pos) ; pos += 1 ; z }
}
[error] found : Int
[error] required: Key
array(pos)这方法貌似是通过key去访问,但是我想通过index去访问这个set,该怎么
写呢?或者有什么办法可以iterate这个map的key集合吗
本人新手,无奈网上找不到,求各位不吝赐教
p*****2
发帖数: 21240
2
转成Array试试
a******3
发帖数: 113
3

def keyIterator: Iterator[Key] = new Iterator[Key] {
val newMap=getMapClone
var array=newMap.keySet().toArray
var pos = 0
def hasNext = pos < newMap.size()
def next: Key = { val z = array(pos) ; pos += 1 ; z }
}
[error] found : z.type (with underlying type Object)
[error] required: Key
[error] def next: Key = { val z = array(pos) ; pos += 1 ; z }
[error] one error found
然后变成获取的内容有问题了。。

【在 p*****2 的大作中提到】
: 转成Array试试
h***i
发帖数: 1970
4
最后那个z换成z.asInstanceOf[Key]

【在 a******3 的大作中提到】
:
: def keyIterator: Iterator[Key] = new Iterator[Key] {
: val newMap=getMapClone
: var array=newMap.keySet().toArray
: var pos = 0
: def hasNext = pos < newMap.size()
: def next: Key = { val z = array(pos) ; pos += 1 ; z }
: }
: [error] found : z.type (with underlying type Object)
: [error] required: Key

1 (共1页)
进入JobHunting版参与讨论
相关主题
问道G 的题问一道C++ template的面试题
讨论一个题目请教 Iterator 一题
binary tree的in-order iterator怎么写?刷了半天题
报个G的电面LC的BST iterator到底要考察什么?
L家的高频题merge k sorted arrays giving iterators求讨论!hash_map 的遍历问题
reverse an array请教个面经里的设计题
问个题问个最近面试里的题目
iterator 实现 如何 peek(),pop()?Two problems from Google
相关话题的讨论汇总
话题: key话题: pos话题: iterator话题: def话题: array