7

整数の配列を、独自に作成したイメージビューのタグと比較しようとしています。

この行を使用して:

if(grid[i][j] == buttons[k].getTag()){

私は正しい軌道に乗っていることを知っていますが、キャストする必要があるのか​​ 、メソッドを使用する必要があるのか​​ わかりません。簡単な質問だと思いますが、どんな助けでも大歓迎です、ありがとう。

4

4 に答える 4

24

タグはオブジェクトなので、次のように入力しIntegerます:

/*
 * UseValueOf
 * ----------
 * Priority: 4 / 10
 * Severity: Warning
 * Category: Performance
 * 
 * You should not call the constructor for wrapper classes directly, such as`new
 * Integer(42)`. Instead, call the valueOf factory method, such as
 * Integer.valueOf(42). This will typically use less memory because common
 * integers such as 0 and 1 will share a single instance.
 */
//MyView.setTag(new Integer(42));
MyView.setTag(Integer.valueOf(42));

次に、次のように値を取得します。

int tagValue = (Integer)MyView.getTag();
于 2012-05-19T15:15:41.850 に答える
12

buttons[k].getTag() を整数に変換する必要があります。

これを行う:

if(grid[i][j] == Integer.parseInt(buttons[k].getTag().toString())){
于 2012-05-19T07:17:54.117 に答える
4

あなたのタグは整数ではなく文字列だと思います。

その場合は、Integer を String() に変換し、equals() かどうかを確認します。

于 2012-05-18T22:35:18.840 に答える