为什么索引在循环之前会增加

 宝宝田小丫 发布于 2023-01-29 13:43

我无法弄清楚为什么for循环计数器在我想要之前/在它执行整个for循环块之前增加.我已经用打印语句乱码代码进行调试,这里是代码:

public void findSubSeq() {
    temp = new ArrayList();
    temp.add(vals.get(0));
    for (int i = 0; i < (vals.size()-2); i++) {
        System.out.println(i + " index in array, for");
        if (vals.get(i) < vals.get(i++)) {
            System.out.println(i + " index in array, if 1");
            temp.add(vals.get(i++));
            System.out.println(i + " index in array, if 2");
            System.out.println(this.getlargest());
        }
        else {
            System.out.println(i + " index in array, else 1");
            compareALs();
            System.out.println(i + " index in array, else 2");
            temp.add(vals.get(i++));
            System.out.println(i + " index in array, else 3");
            System.out.println(this.getlargest());
        }
    }
}

在代码的运行中,我们可以看到计数器i在for循环结束之前增加.我真的很困惑.我也试过了一个for-each循环,结果相似.这是run:在subarrayB创建之前

在填写之前

输入文件:inp

[1,2,3,0,4,5]

在findSubSeq之前

数组中的0索引,用于

数组中的1个索引,否则为1

数组中有1个索引,否则为2

数组中的2个索引,否则为3

[1]

数组中的3个索引,用于

数组中的4个索引,否则为1

数组中的4个索引,否则为2

数组中的5个索引,否则为3

[1]

1 个回答
  • 你有vals.get(i++)哪些增量i.以这种方式使用时,++是一个后增量运算符.

    2023-01-29 13:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有