我有一個整數引用存儲在數組中的值。我如何增加它所指的索引?參考增量索引
int[] pos={0,1,2};
int thisPos=pos[1];
thisPos=pos[++]; //-> thisPos=pos[2];
我有一個整數引用存儲在數組中的值。我如何增加它所指的索引?參考增量索引
int[] pos={0,1,2};
int thisPos=pos[1];
thisPos=pos[++]; //-> thisPos=pos[2];
您需要聲明索引分開:
int index = 1;
int thisPos = pos[index];
index++;
thisPos = pos[index];
你可以使用
pos[2]
如果你想保持一個位置的軌跡,那麼你會希望有一個變量來保存它:
int i = 1;
thisPos = pos[i];
i = i+1;
thisPos = pos[i];
你必須可以讓數組找到該值(並希望數組中的值爲UNIQUE)並獲取其索引,然後增加該索引。例如
curPos = find_index_for_value(pos[1]); // index = 1
curPos = curPos + 1;
thisPos = pos[curPos];
但就像我說過的,你必須確保數組中沒有重複的值。例如如果陣列
{0,1,2,3,4,0,1,2,3,5}
a b c d e f g h i j
和curPos
是2
- 然後其中兩個2
值是你在哪裏? c
或h
?
我覺得你是在誤解這個問題。至少,這不是我對OP想要的印象。 – nhouser9
怎麼這樣? 'thisPos'具有數組中的值,並且OP想要在該值之後移動到下一個數組條目。 –
它不涉及任何索引。 'thisPos'是一個「int」值的容器。就這樣。 –
變量'thisPos'不引用任何索引。它只是包含一個恰好與'pos [1]'中存儲的值相同的值。你不能將它增加到下一個數組元素。你能做的最好的事情就是存儲這個位置,增加這個位置,然後索引到那個位置。 – nhouser9