はあなたにあり、新しい値が見たときに値を追跡していますリスト内でオブジェクトを作成し、値を再初期化します。ここでは、あなたの問題に適用することができるはず例である -
int list[] = {0,0,0,0,0,0,0,0,0,3,3,3,3,3,3,3,1,1,1,1,1,1,1};
int currentVal = list[0];
int start = 0;
int end = -1;
for(int index = 1; index < list.length; index++)
{
if(currentVal != list[index])
{
end = index - 1;
System.out.println("New Object is S: " + start + ", End: " + end + ", Val: " + currentVal);
// create the object here PositionHelper obj = new PositionHelper(start, index - 1, currentVal);
currentVal = list[index];
start = index;
}
}
System.out.println("New Object is S: " + start + ", End: " + (list.length - 1) + ", Val: " + currentVal);
// create the object here PositionHelper obj = new PositionHelper(start, index - 1, currentVal);
出力
New Object is S: 0, End: 8, Val: 0
New Object is S: 9, End: 15, Val: 3
New Object is S: 16, End: 22, Val: 1
注
あなたは、リスト内の非常に最後のオブジェクトを考慮してほしいと、あなたが」勝ちました最後の文がそれを説明するようにforループを終了するまで知りません。 forループの最後の最後をチェックする場合は、別のループを追加することもできます。ディーラーの選択!がんばろう!
'class PositionHelper {int start; int end; int val; } '' List'を繰り返し処理してこのインスタンスを作成する –
私は人々の言うことが好きだStack overflowは 'あなたの任務をあなたに任せましょう'というサービスではありません。元のポスターの試行/思考プロセスについて言及されていない問題文。 – digidude