首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的代码在使用和不使用不必要的代码块时工作不同

我的代码在使用和不使用不必要的代码块时工作不同
EN

Stack Overflow用户
提问于 2019-05-28 00:57:52
回答 1查看 0关注 0票数 0

我有一块我不使用的代码,但它会影响我的输出。

代码语言:javascript
运行
复制
public class Recursion {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(4);
        list.add(6);
        list.add(8);

//        ArrayList<Integer> copy = new ArrayList<>();
//        list.add(2);
//        list.add(4);
//        list.add(6);
//        list.add(8);
        System.out.println(sum(list));

    }

    public static int sum(ArrayList<Integer> array) throws Exception {
        if (array.size() == 1)
            return array.get(0);
        else {
            int x = array.get(0);
            array.remove(0);
            return x + sum(array);
        }
    }
}

给20

没有对“复制”的评论,它给出40,但我不使用它。

EN

回答 1

Stack Overflow用户

发布于 2019-05-28 10:52:27

你打电话list.add给你评论的代码块,而不是copy.add

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档