change index inside a for loop

前端 未结 1 978
北恋
北恋 2021-01-20 00:57

Am I able to change the index inside the for loop in java? For example:

for (int j = 0; j < result_array.length; j++){
            if (item==\" \") {
             


        
相关标签:
1条回答
  • 2021-01-20 01:19

    Yes you can change index inside a for loop but it is too confusing. Better use a while loop in such case.

    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++;
    }
    
    0 讨论(0)
提交回复
热议问题