简体   繁体   English

如何从ArrayList中删除随机元素

[英]How to delete a random element from a ArrayList

I have an ArrayList and i want to do a random to get a element, delete that element and make another random without that deleted element.我有一个 ArrayList 并且我想随机获取一个元素,删除该元素并在没有删除的元素的情况下进行另一个随机。 How can i do that?我该怎么做? I had tried but it doesn't work.我试过了,但它不起作用。

Thanks.谢谢。

MainActivity:主要活动:

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView;

    ArrayList<Integer> list = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView);

        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3);

    }

    public void button(View view) {
        Random rand = new Random();
        int randomElement = list.get(rand.nextInt(list.size()));
        if (list.isEmpty()) {
            textView.setText("Empty");
        } else if (randomElement == 0) {
            textView.setText("Red");
            list.remove(randomElement);
        } else if (randomElement == 1) {
            textView.setText("Blue");
            list.remove(randomElement);
        } else if (randomElement == 2) {
            textView.setText("Yellow");
            list.remove(randomElement);
        } else if (randomElement == 3) {
            textView.setText("Green");
            list.remove(randomElement);
        }
    }
}

I know what you are trying to do, but I suggest a different approach - put colors into the list instead.我知道您要做什么,但我建议采用不同的方法 - 将颜色放入列表中。

public enum Colors {
    GREEN, RED, YELLOW;
}

...

List<Colors> list = new ArrayList<Colors>(Arrays.asList(Colors.values()));
Random rand = new Random();

...

public void button(View view) {
    if (list.isEmpty()) {
        textView.setText("Empty");
    } else {
        Colors randomElement = list.remove(rand.nextInt(list.size()));
        textView.setText(randomElement.name());
    }
}

I think your problem is that remove() is overloaded in ArrayList.我认为您的问题是remove()在 ArrayList 中重载。 It can take either an index (int) or an Object.它可以采用索引 (int) 或对象。 I believe your remove call is actually removing by index, when what you want is to remove by object.我相信您的remove调用实际上是按索引删除,而您想要的是按对象删除。

To solve your problem, I believe you should be able to do remove(Integer.valueOf(randomElement)) to explicitly call the remove(Object) version.为了解决您的问题,我相信您应该能够执行remove(Integer.valueOf(randomElement))来显式调用remove(Object)版本。

On every button function call, the random int gets a limit of the currently available list size.在每次按钮函数调用时,随机整数都会获得当前可用列表大小的限制。 So the random number will be limit to the no of elements present in the array list.因此随机数将限制为数组列表中存在的元素数。

public void button (View view){
  Random r = new Random();
  int randomElement = list.get(r.nextInt(list.size()));

  if (list.isEmpty()) {
    textView.setText("Empty");
  } else {
    switch (randomElement) {
      case 1:
        textView.setText("Red");
        list.remove(randomElement);
        break;
      case 2:
        textView.setText("Blue");
        list.remove(randomElement);
        break;
      case 3:
        textView.setText("Yellow");
        list.remove(randomElement);
        break;
      case 4:
        textView.setText("Green");
        list.remove(randomElement);
        break;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM