나는 운동을 조금하고있다. 기본적으로 4 개의 버튼이 있으며 확인란이 선택되어 있으면 하나를 숨겨야합니다. 이유는 모르겠지만 어떻게해야할지 모르겠다. 배열 대신 arrayList를 만들고 값을 계속해서 제거하거나 값을 '숨기거나 사용하지 않는 다른 방법이 있어야한다. ?Array 또는 ArrayList에서 값 제거 및 추가 (효율적으로) - Android/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
}
}
});
}
}
. 이유 - ArrayList 내부적으로 여전히 배열을 사용합니다. 중간에서 무언가를 제거하는 것은 비효율적입니다. LinkedList는 포인터의 구조를 사용합니다.이 포인터는 반복되는 얼굴로, 물건을 제거하는 데는 훨씬 쉽습니다. 부정적인 점은 랜덤 액세스가 느리다는 것입니다. –
@GabeSechan 오 오케이! 나는 그것에 대해 들어 본 적이 없었습니다. 확실히 확인해 보겠습니다. 감사합니다! – Axelle