i have created class path should uniquely identifiable occupation , last.
case class path(occupation: bitset, last: int) { var cost = 0 def setcost(cost: int) { this.cost = cost } def getcost(): int = { return cost } } } also, sortable count, made field.
implicit val pathord = ordering.by((p: path) => p.getcost) the problem when sorting it(as can see in line above), java.lang.nullpointerexception on line.
why happening? can store data better?
your code gives no exceptions me using code:
@ class path(occupation: bitset, last: int) { var cost = 0 def setcost(cost: int) { this.cost = cost } def getcost(): int = { return cost } } defined class path @ list(new path(bitset(), 3)) res6: list[path] = list(cmd5$path@3ef8de39) @ implicit val pathord = ordering.by((p: path) => p.getcost) pathord: ordering[path] = scala.math.ordering$$anon$9@5f478e42 @ res6.sorted res9: list[path] = list(cmd5$path@3ef8de39) you missing val occupation , last
class path(val occupation: bitset, val last: int) i suggest create case class
case class path(occupation: bitset, last: int) it have equals, , hashcode based on it's fields, tostring, apply, unapply , copy methods.
i not sure if need modify cost, if calculated based on other values make method
case class path(occupation: bitset, last: int) { def cost: int = 42 } if it's not should field. want encourage use immutable structures, mean doing:
case class path(occupation: bitset, last: int, cost: int) adding field setter , getter in scala simple this:
class path(val occupation: bitset, val last: int) { var cost = 0 } you can use this:
val path = new path(bitset(), 3) path.cost = 12 println(path.cost)
Comments
Post a Comment