函数调用堆栈在这段代码中意味着什么?它是如何存储的东西?

时间:2017-06-09 15:55:44

标签: java stack

static void reverse()
{
    if(st.size()>0)
    {
        /* Hold all items in Function Call Stack until we
           reach end of the stack */
        char x = st.peek(); // << This line
        st.pop();
        reverse();

        /* Insert all the items held in Function Call Stack
           one by one from the bottom to top. Every item is
           inserted at the bottom */
        insert_at_bottom(x);
    }
}

代码中<< This line行的含义是什么?代码取自here

2 个答案:

答案 0 :(得分:0)

如果st包含(a1,a2,a3,a4),则此程序调用堆栈(请参阅https://en.wikipedia.org/wiki/Call_stack)将如下所示:

reverse
x = a1
    reverse
    x = a2
        reverse
        x = a3
            reverse
            x = a4
            --> insert_at_bottom(a4)
        --> insert_at_bottom(a3)
    --> insert_at_bottom(a2)
--> insert_at_bottom(a1)

如果我没有正确地发现insert_at_bottom功能,结果st将包含(a4,a3,a2,a1)。

答案 1 :(得分:-1)

boolean isPaused = true; img_scream1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(isPaused){ mediaControl = MediaPlayer.create(java_sounds_scary.this, R.raw.hooray); mediaControl.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.reset(); } }); mediaControl.start(); isPaused = false; } else { mediaControl.pause(); isPaused = true; } } }); 查看此堆栈顶部的对象,而不将其从堆栈中删除。

https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html