Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改for循环内的索引_Java_For Loop_Indexing - Fatal编程技术网

Java 更改for循环内的索引

Java 更改for循环内的索引,java,for-loop,indexing,Java,For Loop,Indexing,我能在java中更改for循环内的索引吗?例如: for (int j = 0; j < result_array.length; j++){ if (item==" ") { result_array[j] = "%"; result_array[j+1] = "2"; result_array[j+2] = "0"; j = j+2;

我能在java中更改for循环内的索引吗?例如:

for (int j = 0; j < result_array.length; j++){
            if (item==" ") {
                result_array[j] = "%";
                result_array[j+1] = "2";
                result_array[j+2] = "0";
                j = j+2;
            }
            else result_array[j] = item;
        }
for(int j=0;j

虽然它在for循环中执行j++,但在for循环中,我也在执行j=j+3。我能做到这一点吗

是的,您可以在for循环中更改索引,但这太令人困惑了。在这种情况下,最好使用while循环

int j = 0;
while (j < result_array.length) {
    if (item.equals(" ")) {
        result_array[j] = "%";
        result_array[j + 1] = "2";
        result_array[j + 2] = "0";
        j = j + 2;
    } else
        result_array[j] = item;
    j++;
}
intj=0;
while(j
不确定您所说的“是否可能?”是什么意思?这是可能的,您已经做到了。这样做在概念上是错误的……在比较字符串(或任何对象)时避免使用
==
是的,但您可能会意外地混淆自己。@MichaelMyers不。。上面的代码是错误的。我应该用while循环