首页 > 解决方案 > java中嵌套的f语句没有返回正确的迭代值?

问题描述

我正在尝试使用 i 遍历 char 数组的“槽”。但是,在循环中的“if”语句之后,“i”值不一致。'i' 的值不是与 'available_slot' 一起增加,而是在 1 和 2 之间交替。我尝试在 if 语句中使用不带 int 的 'u\0000',并尝试在 if 语句的底部添加 i++(无限循环)。有谁知道发生了什么?谢谢!

public int put(String s) {
        int lens = s.length();
        int lena = memoryArray.length;
        int available_slot = 0;
        if (lens > lena){
            return -1;
        }
        for (int i = 0; i < lena; i++) {
            System.out.println(i);

            if ((int)memoryArray[i] == 0) {
                available_slot++;
                System.out.println("got here");
                System.out.println(available_slot);
                System.out.println(i);
            } else {
                available_slot = 0;

            }
            if (lens > lena - i) {
                System.out.println("second if");
                defragment();
                i = 0;

            } else if (available_slot >= lens) {
                System.out.println("third if");
Here is the output I am getting:
0
got here
1
0
1
got here
2
1
2
got here
3
2
second if
1
got here
4
1
2
got here
5
2
second if
1
got here
6
1
third if
-3

标签: javafor-loop

解决方案


推荐阅读