如何洗牌对
如何对成对的元素进行打乱顺序? 下面的程序生成所有可能的对,然后对这些对进行洗牌。 例如,洗牌之前可能的对是 ab,ac,ae,af
..etc 洗牌为 ac,ae,af,ab
...etc
如何使其不仅洗牌成对但在该对本身的元素内? 例如,代替 ab, ac,
我怎样才能制作 ba, ac
?
String[] pictureFile = {"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg","f.jpg","g.jpg"};
List <String> pic1= Arrays.asList(pictureFile);
...
ListGenerator pic2= new ListGenerator(pic1);
ArrayList<ArrayList<Integer>> pic2= new ArrayList<ArrayList<Integer>>();
public class ListGenerator {
public ListGenerator(List<String> pic1) {
int size = pic1.size();
// create a list of all possible combinations
for(int i = 0 ; i < size ; i++) {
for(int j = (i+1) ; j < size ; j++) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(i);
temp.add(j);
pic2.add(temp);
}
}
Collections.shuffle(pic2);
}
//This method return the shuffled list
public ArrayList<ArrayList<Integer>> getList() {
return pic2;
}
}
How to shuffle the elements in the pairs?
The program below, generate all possible pairs and later shuffle the pairs.
e.g. possible pairs before shuffle is ab,ac,ae,af
..etc shuffled to ac,ae,af,ab
...etc
How to make it not only shuffled in pairs but within the elements in the pair itself?
e.g. instead of ab, ac,
how can I make ba, ac
?
String[] pictureFile = {"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg","f.jpg","g.jpg"};
List <String> pic1= Arrays.asList(pictureFile);
...
ListGenerator pic2= new ListGenerator(pic1);
ArrayList<ArrayList<Integer>> pic2= new ArrayList<ArrayList<Integer>>();
public class ListGenerator {
public ListGenerator(List<String> pic1) {
int size = pic1.size();
// create a list of all possible combinations
for(int i = 0 ; i < size ; i++) {
for(int j = (i+1) ; j < size ; j++) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(i);
temp.add(j);
pic2.add(temp);
}
}
Collections.shuffle(pic2);
}
//This method return the shuffled list
public ArrayList<ArrayList<Integer>> getList() {
return pic2;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在将
temp
列表添加到pic2
之前对其进行打乱即可。这是固定代码(请注意,我将pic2
变量转换为ListGenerator
类的字段,并将其重命名为result
)但这只是迈向解决方案的第一步。目前,每对将包含
[0..size-1]
范围内的整数,因此您的对看起来像这样:<0,3>
,< ;1,2> 等。您可能想要的是获得两个字母字符串对,例如:
"ab", "dc"
等。在此版本中我将getList()
重命名为getPairs()
,这样可以更好地传达其含义。另外,我使ListGenerator
的构造函数接受一个字符数组,因此您只需使用所需的字符来调用它,如下所示:这是
ListGenerator
本身:You just have to shuffle the
temp
list before you add it topic2
. Here's the fixed code (note that I turned thepic2
variable into a field of theListGenerator
class and renamed it toresult
)However this is just the first step towards a solution. Currently, each pair will contain integers in the range
[0..size-1]
so your pairs look like this:<0,3>
,<1,2>
, etc. What you probably want is to get a pairs that are two-letter String such as:"ab", "dc"
, etc. In this version I renamedgetList()
togetPairs()
which convey its meaning better. Also, I made the constructor ofListGenerator
accept an array of characters so you just need to call it with your desired characters, as follows:And here is
ListGenerator
it self:假设您有这些对象:
并且您想要对衣服的颜色和物品进行洗牌以获得诸如以下内容:
您是如何做到的?
其实很简单:只需分别打乱颜色列表和服装物品列表,然后再次将它们连接起来即可。
Let's say you have these objects:
And you want to shuffle both the colors and the articles of clothings to get things like:
How do you do it?
It's simple, really: just shuffle the list of colors and articles of clothings separately, and then join them again.