Java—将元素从ArrayList移动到数组
我目前有一个随机混合的Java—将元素从ArrayList移动到数组,java,arrays,arraylist,Java,Arrays,Arraylist,我目前有一个随机混合的ArrayList public static void main(String[] args) { ArrayList<Integer> solution = new ArrayList<>(); for (int i = 1; i <= 48; i++) { solution.add(i); } Collections.shuffle(solution); 新数组包含14个元素的原因是前两个
ArrayList
public static void main(String[] args) {
ArrayList<Integer> solution = new ArrayList<>();
for (int i = 1; i <= 48; i++) {
solution.add(i);
}
Collections.shuffle(solution);
新数组包含14个元素的原因是前两个元素始终相同
heartsRow[0] = 1;
heartsRow[1] = 0;
diamondsRow[0] = 14;
diamondsRow[1] = 0;
spadesRow[0] = 27;
spadesRow[1] =0;
clubsRow[0] = 40;
clubsRow[1] = 0;
我想用
数组列表的非重复元素完全填充每个数组,你可以为循环创建4个,从0到11、12到23、24到35和36到47,并添加到列表中
public static void main(String[] args) {
ArrayList<Integer> solution = new ArrayList<>();
for (int i = 1; i <= 48; i++) {
solution.add(i);
}
Collections.shuffle(solution);
for (int i = 0; i < 12; i++)
heartsRow[i + 2] = solution.get(i);
for (int i = 0; i < 12; i++)
diamondsRow[i + 2] = solution.get(i + 12);
for (int i = 0; i < 12; i++)
spadesRow[i + 2] = solution.get(i + 24);
for (int i = 0; i < 12; i++)
clubsRow[i + 2] = solution.get(i + 36);
for(int i=0;i<12;i++)
炉道[i+2]=溶液。获取(i);
对于(int i=0;i<12;i++)
diamondsRow[i+2]=溶液。得到(i+12);
对于(int i=0;i<12;i++)
spadesRow[i+2]=解决方案。获取(i+24);
对于(int i=0;i<12;i++)
clubsRow[i+2]=解决方案.get(i+36);
您可以为循环创建4个,从0到11、12到23、24到35和36到47,并添加到列表中
for (int i = 0; i < 12; i++)
heartsRow[i + 2] = solution.get(i);
for (int i = 0; i < 12; i++)
diamondsRow[i + 2] = solution.get(i + 12);
for (int i = 0; i < 12; i++)
spadesRow[i + 2] = solution.get(i + 24);
for (int i = 0; i < 12; i++)
clubsRow[i + 2] = solution.get(i + 36);
for(int i=0;i<12;i++)
炉道[i+2]=溶液。获取(i);
对于(int i=0;i<12;i++)
diamondsRow[i+2]=溶液。得到(i+12);
对于(int i=0;i<12;i++)
spadesRow[i+2]=解决方案。获取(i+24);
对于(int i=0;i<12;i++)
clubsRow[i+2]=解决方案.get(i+36);
您可以在列表上使用计数循环,
在每个步骤中,将计数器增加4,
并将元素指定给具有调整偏移的阵列:
for (int i = 0; i + 3 < solution.size(); i += 4) {
int j = i / 4;
heartsRow[2 + j] = solution.get(i);
diamondsRow[2 + j] = solution.get(i + 1);
spadesRow[2 + j] = solution.get(i + 2);
clubsRow[2 + j] = solution.get(i + 3);
}
for(int i=0;i+3
您可以在列表上使用计数循环,
在每个步骤中,将计数器增加4,
并将元素指定给具有调整偏移的阵列:
for (int i = 0; i + 3 < solution.size(); i += 4) {
int j = i / 4;
heartsRow[2 + j] = solution.get(i);
diamondsRow[2 + j] = solution.get(i + 1);
spadesRow[2 + j] = solution.get(i + 2);
clubsRow[2 + j] = solution.get(i + 3);
}
for(int i=0;i+3