RangeRor(rangeRor(索引):无效值:有效值范围为空:-1)
class Stack<T> {
final List<T> _items = [];
//_items declared as Private
void push(T item) => _items.add(item);
T pop() => _items.removeLast();
@override
String toString() => _items.toString();
}
void main() {
final stack = Stack<int>();
stack.push(1);
stack.push(2);
print(stack.pop());
print(stack.pop());
final names = Stack<String>();
names.push('Adam');
names.push('Rose');
print(stack.pop());
}
带有“ stack.pop()”方法的打印命令不会像使用int值一样打印名称值,我该如何修复?
class Stack<T> {
final List<T> _items = [];
//_items declared as Private
void push(T item) => _items.add(item);
T pop() => _items.removeLast();
@override
String toString() => _items.toString();
}
void main() {
final stack = Stack<int>();
stack.push(1);
stack.push(2);
print(stack.pop());
print(stack.pop());
final names = Stack<String>();
names.push('Adam');
names.push('Rose');
print(stack.pop());
}
Print command with the 'stack.pop()' method doesn't print the name values as it does with the int values, How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要执行names.pop()而不是stack.pop()
you need to do names.pop() instead of stack.pop()