English 中文(简体)
Scala - 获取另一套内容的内容(不处理)
原标题:Scala - getting the contents (not address) of another stack
  • 时间:2012-04-30 17:29:12
  •  标签:
  • scala
  • stack

如果有人用 mu子收集,那么,我是否能够复制脚ack,以便我能够用手套,在不改变原来的 st子的情况下分析其内容? 例如,我有一 st,其代码如下:

import scala.collection.mutable.Stack
var stack1 = new Stack[Int]
/** Code that pushes integers on stack1*/
var stackCopy = stack1
while (!stackCopy.isEmpty) {
    println(stackCopy.pop)
}

I want to use a while loop to print all elements in stack1. But when I make a copy and pop off that copy, the original stack (i.e. stack1) is also altered. I want to preserve the original stack, so how can I just get the contents, not the address?

最佳回答

你可以用来理解:

for( i <- stack1 ) { println(i) }

或简单称呼foreach 方法:

stack1 foreach println

如果您坚持使用<代码>,同时。 例:

var is = stack1.toList
while( is.nonEmpty ) {
  println( is.head )
  is = is.tail
}

采用所有这些方法,将保留原来的部分。

问题回答

暂无回答




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

定型语言是否具有范围概念?

定型语言是否具有范围概念? 在我看来,如果职能参数被放在职能执行之前的位置上,这些参数就会以不正统的方式出现。

Stack memory fundamentals

Consider this code: char* foo(int myNum) { char* StrArray[5] = {"TEST","ABC","XYZ","AA","BB"}; return StrArray[4]; } When I return StrArray[4] to the caller, is this supposed to work? ...

negative number in the stack

I am a new student in the compilers world ^_^ and I want to know is legal represent negative number in the stack. For example: infix: 1-5=-4 postfix: 15- The statements are: push(1) push(5) x=...

热门标签