我有一个包含9个值的数组。我想用随机的那些值填充另一个大小为18的数组,关于它们应该只发生两次。
String[] cards = {"1","2","3","4","5","6","7","8","9"};
答案 0 :(得分:0)
对于懒惰的人来说这个问题。
这是我的LazyRandomizer类的结果。
[7, 5, 4, 7, 9, 3, 8, 1, 1, 3, 5, 4, 6, 2, 6, 2, 8, 9]
这是代码。通常,我会解释代码,但我现在感觉太懒了。祝你好运。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LazyRandomizer {
public static void main(String[] args) {
String[] cards = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
List<String> randomCards = new ArrayList<>();
randomCards.addAll(Arrays.asList(cards));
randomCards.addAll(Arrays.asList(cards));
Collections.shuffle(randomCards);
System.out.println(Arrays.toString(randomCards
.toArray(new String[randomCards.size()])));
}
}