java - 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
解决方案
推荐阅读
- angular - @ViewChild 注释的使用在我的代码中返回 undefined
- c# - 如何在 xamarin 中使用 xaml 中的类属性?
- python - 即使在获取数据后,Django 管理面板 list_display 也不显示
- mysql - 主索引与索引的查询性能
- php - 如何修复由于从数据库 blob 中检索大图像而导致的不完整 html 加载
- android - Gradle 同步失败:无效类型代码:6C
- r - 使用自动绘图标记值
- python - MSTSC 使用带有预填充目标或直接连接的 python
- java - 如何从 Android 应用程序调用 Java Web 服务?
- vue.js - 在模板上使用 SELECT 标记时,控制台多次显示错误“TypeError:this.each 不是函数”