简体   繁体   English

有人可以解释一下该程序在堆栈中的工作方式吗?

[英]Can somebody explain me the working of this program with stacks?

I have been very keen to get some hands on recursion but i seem to misunderstand it 我一直很想尝试递归,但是我似乎误会了它

i have read a question in a book and i am a bit confused and i don't want to cramp it , I want some solid explanation with stacks also about calling of the functions also 我已经读过一本书中的一个问题,我有点困惑,我不想局促一下,我想对堆栈进行一些扎实的解释,同时也要介绍函数的调用

class rectest
{
int values[];

rectest(int i)
{
    values = new int[i]; 
}

 void printarray(int i)
  {
    if(i==0)
        return ; 
    else printarray(i-1);
    System.out.print(values[i-1]+" ");
  }
 }

public class recursion 
{
  public static void main(String args[])
   {
    rectest ob = new rectest(10);
    int i ; 
    for(i=0 ; i<10 ; i++)
        ob.values[i] = i ;
    ob.printarray(10);
   }
}

RECURSION Means Repetition calling of anything again and again RECURSION意味着重复调用任何东西

This is one of the Traditional and important program to learn how Recursion works and ? 这是了解递归工作原理的传统且重要的程序之一,并且? is Recursion and Why Let me take an example of Calculating Factorial thier pseudocode will be like this 是递归,为什么让我举一个计算阶乘伪代码的示例,就像这样

function factorial is: 函数阶乘为:

input: integer n such that n >= 1
output: [n × (n-1) × (n-2) × … × 1]

    1. if n is >= 1, return [ n × factorial(n-1) ]
    2. otherwise, return 1

end factorial

now what happens here is that it always return [n*factorial(n-1)] which call itself over and over again 现在,这里发生的是,它总是返回[n * factorial(n-1)],并一遍又一遍地调用自己

Now Let us consider Your Context 现在,让我们考虑一下您的上下文

 public class recursion 
    {
      public static void main(String args[])
       {
        rectest ob = new rectest(10);

//here you are initializing the object and calling its constructor and intializing the array having size of 10 //在这里初始化对象并调用其构造函数并初始化大小为10的数组

int i ; 
for(i=0 ; i<10 ; i++)
    ob.values[i] = i ;

//Here you are assigning every member variable to a certain values like at position 0 value[0]=0 //在这里,您将每个成员变量分配给某个值,例如位置0 value [0] = 0

ob.printarray(10); ob.printarray(10); //here u are printing the values of ten values which you have passed now Important thing occurs here //这里您正在打印您现在已传递的十个值的值重要的事情在这里发生

} } Now Look at the printarray() Method here 现在在这里查看printarray()方法

void printarray(int i)
  {
    if(i==0)
        return ; 
    else printarray(i-1);
//printarray(i-1) here it calls the method itself so as to print all the values recursively 
    System.out.print(values[i-1]+" ");
  }

thats it if you have more query ask 就是这样,如果您有更多查询询问

If you have to understand about recursion you can look at this stack overflow question. 如果您必须了解递归,可以查看此堆栈溢出问题。 What is recursion and when should I use it? 什么是递归,什么时候应该使用它?

If you want to have better understanding of implementation of recursion in java here is another stack overflow question. 如果您想更好地理解Java中递归的实现,这里是另一个堆栈溢出问题。 How is recursion implemented in Java 如何在Java中实现递归

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM