2016-09-29 3 views
0

나는 운동을 조금하고있다. 기본적으로 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 
       } 
      } 
     }); 
    } 
} 
+0

. 이유 - ArrayList 내부적으로 여전히 배열을 사용합니다. 중간에서 무언가를 제거하는 것은 비효율적입니다. LinkedList는 포인터의 구조를 사용합니다.이 포인터는 반복되는 얼굴로, 물건을 제거하는 데는 훨씬 쉽습니다. 부정적인 점은 랜덤 액세스가 느리다는 것입니다. –

+0

@GabeSechan 오 오케이! 나는 그것에 대해 들어 본 적이 없었습니다. 확실히 확인해 보겠습니다. 감사합니다! – Axelle

답변

1

당신은 옵션의 톤을 가지고 : :)

여기 (수입 제외) 코드 MainActivity.java 코드입니다. 제안한 것처럼 ArrayList를 사용할 수 있습니다. 사용 가능한 색상 목록을 setColor 메서드에 전달할 수 있습니다. 어쩌면 너는 사용하고 싶지 않은 색을 무작위로 전달할 수있다. if(randomedColor == colorYouDontWant) then random again. Map<String, Color>을 사용하고 거기에 모든 색상을 넣은 다음이 맵에서 삭제하면 무작위로 분류 될 수 있습니다. 또는 Map<String, Boolean> 여기서 key는 color이고 value는 color가 사용 가능한지 여부입니다 (사용 가능한 경우 true, 그렇지 않은 경우 false). ArrayList를 사용하는 것이 좋습니다.

btw. 이 조각 :

if(rndColor2 == rndColor){ 
    rndColor2 = randomColor(colors.length); 
} 

색상을 동일하게하고 싶지는 않지만, 임의로 다시 동일한 결과를 얻으려면 어떻게해야합니까? 당신은 어떻게해야 : 당신이 목록을 사용하는 경우, 배열 목록을 사용하십시오 LinkedList의를 사용하지 않는

while(rndColor2 == rndColor)

+0

와우 정말 고마워, 그게 전부 좋은 제안이야! ArrayList 또는 setColor 매개 변수 솔루션으로 시작할 것이라고 생각합니다. ArrayList가 무서워서 추가하고 제거하기에 약간의 중복이 될 것입니다. while 루프도 통합 할 것입니다. 완전히 맞습니다. 고맙습니다 :) – Axelle