Tag: 堆栈

Kotlin:MutableList中的java.lang.UnsupportedOperationException添加元素

我在Kotlin中实现了一个用于学习目的的堆栈算法 class Stack<T:Comparable<T>>(list:MutableList<T>) { var items: MutableList<T> = list fun isEmpty():Boolean = this.items.isEmpty() fun count():Int = this.items.count() fun push(element:T) { val position = this.count() this.items.add(position, element) } override fun toString() = this.items.toString() fun pop():T? { if (this.isEmpty()) { return null } else { val item = this.items.count() – 1 return this.items.removeAt(item) } } fun peek():T? […]