简体   繁体   English

当我使用“n--”而不是“n-1”时,为什么会出现 stackoverflow 错误?

[英]When I use "n--" instead of "n-1" why do I get stackoverflow error?

In this code which is a program to print elements 1 to 5 recursively, when I use n-- instead of n-1 , I am getting a stack overflow error.在这段代码中,它是一个递归打印元素 1 到 5 的程序,当我使用n--而不是n-1时,我得到一个堆栈溢出错误。 whereas when n-1 is used the code worked perfectly.而当使用n-1时,代码运行良好。 shouldn't n-- and n-1 work the same in this code?不应该n--n-1在这段代码中工作相同吗?

    //when using n--

    class PrintElements1to5
    {
        public static void main(String[] args)
        {
            recursivePrint(5);
        }
    
        static void recursivePrint(int n)
        {
            if(n<1)
                return;
            recursivePrint(n--);  
            System.out.print(n+" ");
        }
    }

output:输出:

Exception in thread "main" java.lang.StackOverflowError
    //when using n=n-1
    class PrintElements1to5
    {
        public static void main(String[] args)
        {
            recursivePrint(5);
        }
    
        static void recursivePrint(int n)
        {
            if(n<1)
                return;
            recursivePrint(n-1);
            System.out.print(n+" ");
        }
    }

output:输出:

1 2 3 4 5 

n-- returns the value of n first, then decrement n . n--返回n的值,然后递减n So in your case, it becomes an infinite loop because n is never changing.所以在你的情况下,它变成了一个无限循环,因为n永远不会改变。 You can use --n instead which decrement n first, then returns the value of n .您可以使用--n而不是先递减n ,然后返回n的值。 Let's take a simpler example.让我们举一个更简单的例子。

int x = 3;
System.out.println(x - 1); // prints 2, x is still 3
System.out.println(x--);   // prints 3, x becomes 2
System.out.println(--x);   // prints 1, x becomes 1

暂无
暂无

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

相关问题 为什么需要提及方法名称? 为什么不简单地(n-1)* n? - Why do I need to mention the method name? Why not simply (n-1)*n? 为什么会出现Stackoverflow错误? - Why do I get a Stackoverflow Error? 为什么我会收到此递归 stackoverflow 错误? - Why do I get this recursion stackoverflow error? 我如何编写递归方法来求和 x^n + x^(n-1) + x^(n-2)? - How do i write a recursive method to sum x^n + x^(n-1) + x^(n-2)? 为什么执行INTO OUTFILE查询时得到\\ N? - Why do I get \N when executing INTO OUTFILE query? 为什么使用i +(int)(Math.random()*(ni))而不是(int)(Math.random()* n)? - Why use i + (int) (Math.random() * (n-i)) instead of (int) (Math.random() * n)? 为什么即使使用@JsonIgnoreProperties,在使用杰克逊时也会出现stackoverflow错误 - Why do i get an stackoverflow error when using jackson even though using @JsonIgnoreProperties 为什么我在Windows中将\\ r \\ r \\ n作为换行符而不是\\ r \\ n作为换行符char - Why I get \r\r\n as newline instead of \r\n as newline char in Windows 如果我在 recyclerview 中添加 n 个数据,它只显示 n-1 个数据。当我添加第 n+1 个数据时,它显示第 n 个数据 - If I am adding n data in recyclerview it is displaying n-1 data only.it is showing n th data when I add the n+1 th data 延迟加载多对多实体时,为什么会收到N + 1个选择查询 - Why do I get N+1 select queries when lazy loading many-to-many entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM