配列内の値を見つけようとしていますが、この値が1回だけ存在することは確かなので、値を見つけて、それが格納されている配列のインデックスを返そうとしています。見つからない場合は、-1を返します。私がやろうとしていることです:
static Integer[] accs = new Integer[20];
public static int search()
{
Integer[] numbers;
numbers = accs;
Integer key;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Account Number:");
key = sc.nextInt();
for (Integer index = 0; index < numbers.length; index++)
{
if ( numbers[index] == key )
return index; //We found it!!!
}
// If we get to the end of the loop, a value has not yet
// been returned. We did not find the key in this array.
return -1;
}
値が配列に存在することがわかっていてもこれを実行すると、何も表示されません。デバッグしたところ、Key変数に入力した値が含まれていないことがわかりました。何か問題がありますか?