Jpixta
Jpixta

Reputation: 101

How to remove a string from an arraylist randomly

So what this program is supposed to do is the user inputs a name into a text box and then presses a button to add it into an array which is displayed onto a text area. The thing is that I can't figure out how to take away a name from the arraylist randomly. I am a noob programmer.

Here is what I have so far.

 Random r = new Random();
 ArrayList <String> names = new ArrayList<String>();

 private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       

    names.add(txtAdd.getText());
    txtDisplay.setText("" + names);
 }      

Upvotes: 3

Views: 483

Answers (1)

nrubin29
nrubin29

Reputation: 1612

names.remove(r.nextInt(names.size()));

Upvotes: 5

Related Questions