2014-10-27 146 views
0

的名单阵列我有这个类:初始化对

abstract class SimpleHashTable extends HashTable { 
    type Bucket = List[Pair[K, V]] 
    type Table = Array[Bucket] 

    def alloc_data(size: Int): Table = { 
     var tb = new Table(size) 
     //tb.apply(_ => Nil) 

我不知道如何将Table的内容初始化为Nil

请指教。谢谢,

回答

5

使用var tb = Array.fill(size)(List[Pair[K,V]]())

scala> type Bucket = List[Pair[Int, String]] 
defined type alias Bucket 

scala> type Table = Array[Bucket] 
defined type alias Table 

scala> val tb:Table = Array.fill(5)(List[Pair[Int, String]]()) 
tb: Table = Array(List(), List(), List(), List(), List()) 

编辑: 其实这句法可能是有点更容易阅读:

scala> val tb:Table = Array.fill(5)(Nil) 
tb: Table = Array(List(), List(), List(), List(), List()) 
+0

或'List.empty'。 – 2014-10-27 09:36:44