递归和迭代有什么区别? [英] What is the difference between recursion and iteration?

查看:13
本文介绍了递归和迭代有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找java中递归和迭代之间的区别.请包含两者的 Java 代码示例.

I am searching for the difference between recursion and iteration in java. Please include a Java code example of both.

2.如果我在android中收到消息,我怎样才能打开我打开的应用程序?

2.How can i open my open application only if i receive message in android?

  1. 如何关闭设备中运行的第三方服务?

推荐答案

当方法或函数在其原始参数的子集上调用自身时,就会发生递归.例如,MergeSort - 它将数组分成两半并在这两半上调用自身.

Recursion happens when a method or function calls itself on a subset of its original argument. For example, MergeSort - it splits the array into two halves and calls itself on these two halves.

递归看起来像这样,但它是一个非常人为的示例,其工作方式类似于下面的迭代示例:

Recursion would look like this, but it is a very artificial example that works similarly to the iteration example below:

public void doSomething(int i) {
    // code
    if (i == 10) {
        return;
    } else {
        doSomething(i + 1);
    }
}

迭代是循环或循环的一次通过.比如这个循环中的代码:

Iteration is one pass of a cycle or loop. For example, code in this loop:

for(int i = 0; i < 10; i++){
    //code
}

将执行 10 次,即有 10 次迭代.

will be executed 10 times, i.e. have 10 iterations.

这篇关于递归和迭代有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆