guliing guliing - 2 months ago 7
Java Question

Randomize from a made list

ArrayList<String> listArray = new ArrayList<String>();

private void AddToListActionPerformed(java.awt.event.ActionEvent evt) {
String addTo = txtAdd.getText();
listArray.add(addTo);
}

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

}


In the button randomPicker, I want to print out one of the random strings I added in my array. Can you guys help me out with what I should write inside that button?

I tried:

String random = (listArray[new Random().nextInt(listArray.length)]);
System.out.println(random);


It said i couldn't use
nextInt

Answer

you can do like this

Random ran = new Random();
String random = (String) list.get(ran.nextInt(list.size()));
System.out.println("random :"+rdom);
Comments