-3

How can i pick a random char from azL and then put it in another list?

char[] azL = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'};

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

    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int Plength = 6;
            for (int x = 0;x < Plength;x++){

            }
           // textV.setText();
        }
    });
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Dubli
  • 11
  • 4
  • Look for `Random` class https://docs.oracle.com/javase/7/docs/api/java/util/Random.html – Jérôme Apr 11 '17 at 09:13
  • 1
    Possible duplicate of [How to generate random integers within a specific range in Java?](http://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java) – Jérôme Apr 11 '17 at 09:14

2 Answers2

1

You can generate a random number with

int randomIndex = (int)(Math.random()*list.size());

Then you can pick that random element and add it to a new List with

newList.add(list.get(randomIndex));
Markus
  • 1,141
  • 1
  • 9
  • 25
1

Use Random class to generate a random integer in the required range, i.e. 0 to azl.length. Then get the element at the random index from the array and put it in the other list.

Random random = new Random();
int randomIndex = random.nextInt(azl.length);    // [0, azl.length-1]
otherList.add(azl[randomIndex]);
iavanish
  • 509
  • 3
  • 8