私は作っているエクササイズにちょっと固執しています。 私は基本的に4つのボタンを持っていて、チェックボックスがチェックされていれば1つを隠す必要があります。 なぜか分かりませんが、配列の代わりにarrayListを作成し、値を絶えず削除または追加するか、値を使用しない、または使用しない方法があります?ArrayまたはArrayListの値を削除して追加する(効率的に) - Android/Java
希望は私の説明は少し明確です、事前に感謝! :)
は、ここで(輸入品を含まず)コードMainActivity.javaコードです:
public class MainActivity extends AppCompatActivity {
String globalColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setColor();
}
private int randomColor(int length){
return (int) Math.floor(Math.random()*length);
}
private void setColor(){
String [] colors = {"Green", "Blue", "Red", "Magenta"};
//ArrayList<String> colors = new ArrayList<String>();
int rndColor = randomColor(colors.length); //color name for text
int rndColor2 = randomColor(colors.length); //color for text color
if(rndColor2 == rndColor){
rndColor2 = randomColor(colors.length);
}
globalColor = colors[rndColor];
TextView v = (TextView) findViewById(R.id.color);
v.setText(colors[rndColor]);
v.setTextColor(Color.parseColor(colors[rndColor2]));
}
public void checkColor(View v){
Button b = (Button)v;
String buttonText = b.getText().toString();
TextView txtview = (TextView) findViewById(R.id.result);
if(buttonText.equals(globalColor)){
txtview.setText("Yaay");
setColor();
} else {
txtview.setText("Booo");
setColor();
}
}
private void hideMagenta(){
CheckBox checkbox = (CheckBox)findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
//this is where my problem is; I want to remove magenta as an option
//is it better to do this with an arraylist and to remove and add magenta to the arraylist
//or is there a different, more efficient way
}
}
});
}
}
を、LinkedListのを使用して、配列リストを使用しないでください。理由 - ArrayListは内部的にも配列を使用します。真ん中から何かを取り除くことは非効率的です。 LinkedListはポインタの構造を使用しています。つまり、反復処理を行うときと同じように、そして削除するのがはるかに簡単です。その否定は、ランダムアクセスが遅いということです。 –
@GabeSechan oh ok!私はそれを聞いたことがないだろう、私は間違いなくそれをチェックします、ありがとう! – Axelle