配列内の隣接する要素の束を数えるにはいくつかの助けが必要です。配列内の要素の束Java
私は4つの整数配列を持っていると言います。
myArray = {1, 1, 1, 1}
myArray2 = {5, 6, 7, 8, 7, 1, 10, 11, 12};
myArray3 = {5, 6, 7, 7, 8, 1, 10, 11, 12};
myArray4 = {5, 6, 7, 7, 8, 1, 10, 11, 11, 11, 12};
これらの配列の要素数を計算します。
所望の出力は次のようになります。
myArray => 1 (1 bunch of adjacent element "1")
myArray2 => 0 (no adjacent element)
myArray3 => 1 (1 bunch of adjacent element "7")
myArray4 => 2 (2 bunches of adjacent elements "7" and "11")
私は隣接する要素ではなく、房をカウントすることができます。
は、ここで私が書いたコードです:私は宿題をしたくない
import acm.program.ConsoleProgram;
public class project extends ConsoleProgram{
public void run()
{
int[] myArray = {1, 1, 1, 1};
int[] myArray2 = {5, 6, 7, 8, 7, 1, 10, 11, 12};
int[] myArray3 = {5, 6, 7, 7, 8, 1, 10, 11, 12};
int[] myArray4 = {5, 6, 7, 7, 8, 1, 10, 11, 11, 11, 12};
println("myArray -> " + countBunches(myArray));
println("myArray2 -> " + countBunches(myArray2));
println("myArray3 -> " + countBunches(myArray3));
}
public int countBunches(int[] myArray)
{
int count = 0;
int saveIndex;
int saveContent;
for(int i = 0; i<myArray.length; i++)
{
if(i == 0)
{
saveContent = myArray[i];
}
else
{
saveContent = myArray[i-1];
if(saveContent == myArray[i])
{
saveContent = myArray[i];
saveIndex = i;
count++;
println(myArray[saveIndex] + " repatingb" + saveIndex + ". element");
}
else
{
saveContent = myArray[i];
}
}
}
return(count);
}
}
申し訳ありませんが、「したいですが」は質問ではなくコード要件です。あなたの質問を編集し、あなたのコードを書くのを止めている特定の問題の説明を記入してください。また、あなたの試行を含めて、他の人があなたを助けるよう促すかもしれません。 – Pshemo
@Pshemo私は私の試みを加えました。 "私は欲しい"と申し訳ありません。 –
@freedev私は今日以来試みてきました。そして、SOはその問題に関する私の最新のステーションです。 –