class CustomStack<T> {
final _list = <T>[];
void push(T value) => _list.add(value);
T pop() => _list.removeLast();
T get top => _list.last;
bool get isEmpty => _list.isEmpty;
bool get isNotEmpty => _list.isNotEmpty;
int get length => _list.length;
@override
String toString() => _list.toString();
}
void main() {
CustomStack<String> plates = CustomStack();
//Add plates into the stack
plates.push("Plate1");
plates.push("Plate2");
plates.push("Plate3");
plates.push("Plate Extra");
print(plates);
print(plates[plates.length-1]);
}
我在最后一行出現錯誤“未為型別 'CustomStack' 定義運算子'[]'。” 如何控制堆疊中的索引。我只想在螢屏上列印“Plate Extra”。
uj5u.com熱心網友回復:
plates[plates.length-1]如果可以使用內置函式獲取最后一個元素,則無需使用該結構。如果要獲取 中的最后一項Custom Stack,可以在Custom Stack.
T get peek => _list.last;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520971.html
標籤:扑镖索引堆
