jeudi 6 mai 2021

Select a every 5 elements- java [duplicate]

I have an array list that looks like this.

8.1,6.5,4.4,3.2,1,8.9,5,1.4,0.1,1,8.7,6.2,4.3,3.2,3

I would like that my program selects every 5 numbers randomly.

for example to select

8.1,6.5,4.4,3.2,1,8.7,6.2,4.3,3.2,3

as you can see it selected 5 numbers from the begging and 5 more from the end

and saves them

This is what I have

Random random_method = new Random();

// loop for generation random number
for (int i = 0; i < array.size(); i++) 
{
    // generating random index with the help of
    // nextInt() method
    int index = random_method.nextInt(array.size());

    System.out.println("Random Element is :" + array.get(index));

}

I have this but that not doing what I need




Aucun commentaire:

Enregistrer un commentaire