0

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. 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);
        }
    }
}
2
  • hard to understand what is your question ? Commented Feb 8, 2018 at 17:12
  • use listIterator Commented Feb 8, 2018 at 17:14

3 Answers 3

5

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I think your problem is that remove() is overloaded in ArrayList. It can take either an index (int) or an Object. I believe your remove call is actually removing by index, when what you want is to remove by object.

To solve your problem, I believe you should be able to do remove(Integer.valueOf(randomElement)) to explicitly call the remove(Object) version.

2 Comments

or you can just type cast . list.remove((Integer)randomElement);
Yes. Now that I look at it, he could probably just declare randomElement as Integer as well and not unbox it.
0

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;
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.