Random string from string array list

Try:

list[r.nextInt(list.length)];

The accepted answers is not working for me the solution worked for me is

List<String> myList = Arrays.asList("A", "B", "C", "D");

Suppose you have this above ArrayList and you want to randomize it

    Random r = new Random();

    int randomitem = r.nextInt(myList.size());
    String randomElement = myList.get(randomitem);

If you print this randomElement variable you will get random string from your ArrayList


static String getRandomString(){
        int r = (int) (Math.random()*5);
        String name = new String [] {"India","USA","UK","Russia"}[r];
        return name;
    }

Tags:

Java

Android