0

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 1 on the records array, I can use Integer.parseInt(records[1])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 place it without getting an array index out of bounds exception thrown in my face. 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-
Dilawer
  • 35
  • 6
  • Possible duplicate of [Random shuffling of an array](http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – mkobit Oct 09 '15 at 17:49
  • I'm not using numbers. I am using one array of strings which includes the names and group sizes. – Dilawer Oct 09 '15 at 17:51
  • 1
    Not a duplicate of Random shuffling, because he wants to keep the groups together. This probably a bad data structure to use for this. You need a two-dimensional data structure. Then you can just shuffle the outer array / ArrayList, etc. – Richard Peterson Oct 09 '15 at 17:52

2 Answers2

1

It looks to me like you are simply using the wrong data type, which is making your life unnecessarily complicated. Rather than a String array with codes to indicated group sizes, you could have:

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

Even better would be to use an ArrayList<String[]> or an ArrayList<List<String>> because then you could just do Collections.shuffle(records);

mkobit
  • 43,979
  • 12
  • 156
  • 150
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
0

There are numerous ways to do this but all rely on similar concepts, just "hide" them differently.

  1. You randomly pick an element you didn't previously pick from the source array and you just place it at the next position in the destination, in order. You are done when you reach the end of the destination array / you processed enough elements. You generate random numbers repeatedly, if necessary, until one lands on the element you didn't pick. How do you know if you picked it already? You have to have a way to recognize this. For example, if you know you won't have null values, you can overwrite the source array element with null once you pick it. If you land on null, you know you need another random number. Another way is having a separate array of flags.
  2. You randomly pick an element from the source array and put it in the destination. You then remove it from the source array and shift the elements after it one position forward. Repeat until you reach the end of the destination (you process the correct count of elements).
    1. Copy source elements into the destination, in order. Then generate pairs of random indexes and switch places of the values at these indexes. Think of it as shuffling. Repeat as you see fit, but no less times than N/2 where N is the array size. You can go crazy with how you pick the two positions to swap. ...
Learner
  • 1,215
  • 1
  • 11
  • 26