public boolean setValidColor(String input, String colors) {
int exists;
isValidColor = true;
char[] colorch = colors.toCharArray();
Arrays.sort(colorch);
for(int i = 0; i < input.length(); i++)
{
exists = Arrays.binarySearch(colorch, input.charAt(i));
if(exists == -1)
{
isValidColor = false;
break;
}
}
return isValidColor;
}
私は、異なる長さの2つの文字列を比較し、無効な入力のわずか1つのインスタンスでfalseを返すのに問題があります。
例えば: 可能な色はRGOPYBであり、入力色はYZORです。
‘Z’は可能ではないので、私はfalseを返すようにコードを書く必要がありますが、コードは真のままです。どこが間違っていますか?
編集:このコード(OOPをやっている)よりも多くのコードがありますが、これは私が問題に陥っている単なる方法です。
ベストアンサー
Arrays.binarySearch
will only return -1 if the
value being searched for should be inserted at position 0. For
other values that are not found, it returns other negative values.
Test for exist < 0
instead of exists ==
.
-1