2

I've got an array with 225 elements and 15 smaller arrays which their length sum is exactly 225.

The point is that I need to fill the larger array with these smaller arrays but in a random way.

 private final short datosdeNivel[]= new short[225];
 private final short diecinueve[]= {19, 19};
 private final short veintiseis[]= {26, 26, 26};
 private final short dieciocho[]= {18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18};
 private final short veintidos[]= {22, 22};
 private final short veintiuno[]={21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
 private final short cero[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 private final short diecisiete[]= {17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
 private final short dieciseis[]= {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
 private final short veinte[]= {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
 private final short veinticuatro[]= {24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
 private final short veinticinco[]= {25, 25, 25, 25};
 private final short veintiocho[]= {28, 28};
 private final short uno[]= {1, 1, 1, 1, 1, 1, 1};
 private final short nueve[]= {1};
 private final short ocho[]= {9, 9, 9, 9, 9, 9, 9, 9, 9};

How can I establish a random order so every time the program runs the order in which the smaller arrays are placed in the larger array is different?

This would be a way to fill it in order:

int aux;

        aux= diecinueve.length;

        for(int i=0; i<diecinueve.length; i++)
        {
            datosdeNivel[i]= diecinueve[i];
        }
        for(int i=0; i<veintiseis.length; i++)
        {
            datosdeNivel[aux]= veintiseis[i];
            aux++;
        }
        for(int i=0; i<dieciocho.length; i++)
        {
            datosdeNivel[aux]= dieciocho[i];
            aux++;
        }
        for(int i=0; i<veintidos.length; i++)
        {
            datosdeNivel[aux]= veintidos[i];
            aux++;
        }
        for(int i=0; i<veintiuno.length; i++)
        {
            datosdeNivel[aux]= veintiuno[i];
            aux++;
        }
        for(int i=0; i<cero.length; i++)
        {
            datosdeNivel[aux]= cero[i];
            aux++;
        }
        for(int i=0; i<diecisiete.length; i++)
        {
            datosdeNivel[aux]= diecisiete[i];
            aux++;
        }
        for(int i=0; i<dieciseis.length; i++)
        {
            datosdeNivel[aux]= dieciseis[i];
            aux++;
        }
        for(int i=0; i<veinte.length; i++)
        {
            datosdeNivel[aux]= veinte[i];
            aux++;
        }
        for(int i=0; i<veinticuatro.length; i++)
        {
            datosdeNivel[aux]= veinticuatro[i];
            aux++;
        }
        for(int i=0; i<veinticinco.length; i++)
        {
            datosdeNivel[aux]= veinticinco[i];
            aux++;
        }
        for(int i=0; i<veintiocho.length; i++)
        {
            datosdeNivel[aux]= veintiocho[i];
            aux++;
        }
        for(int i=0; i<uno.length; i++)
        {
            datosdeNivel[aux]= uno[i];
            aux++;
        }
        for(int i=0; i<nueve.length; i++)
        {
            datosdeNivel[aux]= nueve[i];
            aux++;
        }
        for(int i=0; i<ocho.length; i++)
        {
            datosdeNivel[aux]= ocho[i];
            aux++;
        }

3 Answers3

1

You could try creating an ArrayList, containing all of your smaller arrays:

ArrayList<short[]> arrays = new ArrayList<>();
arrays.add(ocho);
arrays.add(veintiocho);
// ...

and then access indexes randomly, removing from the list each time you add to your large array:

Random rand = new Random();
while (!arrays.isEmpty()) {
    int index = rand.nextInt(array.size());
    short[] s = arrays.get(index);

    for(int i = 0; i < s.length; i++)
    {
        datosdeNivel[aux]= s[i];
        aux++;
    }

    arrays.remove(index);
}        

This will add each array to the large array in a random order.

Kevin Grant
  • 591
  • 6
  • 15
1

In pseudo code:

  • Put the arrays in a List
  • Shuffle the list using Collections.shuffle(list)
  • iterate over the list, filling the final array with elements of each array
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

If I were you, I'll give each array an index, then make a bucket of these indexes, then randomly pick up an index from the bucket, do your copy job, and remove this used index from your bucket.

antonio081014
  • 3,553
  • 1
  • 30
  • 37