vendredi 9 octobre 2015

Taking group elements from one array and placing them randomly in another array

I have two arrays, both String type but one is null

String[] records = {"10", "2", "John", "Chris", "1", "Peter", "3", "Bob", "George", "Paul"};

String[] seating = (Integer.parseInt(records[0])); //Size of the array is the 0 index of the records.

I am able to get the index positions and group sizes for example index 0 on the records array, I can use Integer.parseInt(records[0])to get the size and using that also find out the index for the next group.

My question is: How would I place the group randomly into the new array & how would I determine when to stop. Without overwriting a previously placed group. I am able to generate random numbers each time of the iteration(each iteration places a group). I also have a method which take the index and the arrays and the random number generated which places it in the new array.

Each time I run the program it places the groups randomly. So for example my output might be (empty spots contain "-empty-").

Spot 1: -empty-
Spot 2: Bob
Spot 3: George
Spot 4: Paul
Spot 5: -empty-
Spot 6: John
Spot 7: Chris
Spot 8: -empty-
Spot 9: Peter
Spot 10: -empty-




Aucun commentaire:

Enregistrer un commentaire