如何以随机方式用较小的数组填充数组?

时间:2016-05-25 23:43:26

标签: java arrays random

我有一个包含225个元素和15个较小数组的数组,它们的长度总和恰好是225.

重点是我需要用随机的方式用较小的数组填充较大的数组。

 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};

如何建立随机顺序,以便每次程序运行时,较小数组放在较大数组中的顺序是不同的?

这将是一种按顺序填写它的方法:

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 个答案:

答案 0 :(得分:1)

您可以尝试创建一个ArrayList,其中包含所有较小的数组:

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

然后随机访问索引,每次添加到大型数组时从列表中删除:

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);
}        

这会以随机顺序将每个数组添加到大数组中。

答案 1 :(得分:1)

在伪代码中:

  • 将数组放入List
  • 使用Collections.shuffle(list)
  • 随机播放列表
  • 遍历列表,使用每个数组的元素填充最终数组

答案 2 :(得分:0)

如果我是你,我会给每个数组一个索引,然后创建一个这些索引的存储桶,然后从存储桶中随机选取一个索引,执行复制作业,并从存储桶中删除这个使用过的索引。 / p>