[英]How to print combinations of Strings from String arrays in an ArrayList (Java)
So I have an ArrayList<String[]>
list which contains some String[]
that I need to get the combinations of. 所以我有一个
ArrayList<String[]>
列表,其中包含一些String[]
,我需要获取它们的组合。
So if list contained A = {a,b,c}
and B = {d,e,f}
I want to print 因此,如果列表包含
A = {a,b,c}
和B = {d,e,f}
我想打印
ad
, ae
, af
, bd
, be
, bf
, cd
, ce
, cf
ad
, ae
, af
, bd
, be
, bf
, cd
, ce
, cf
I am just having trouble seeing how I would have one fixed array then get the other so I can iterate through that. 我只是很难看到我将如何拥有一个固定的阵列然后得到另一个固定的阵列,因此我可以遍历该阵列。
Edit: To clarify I am trying to do this for N String[]
so I cant just get the 0th and 1st element. 编辑:澄清一下,我正在尝试为N
String[]
做这个,所以我不能只得到第0个和第1个元素。
Iterate over two arrays and on each step make new string combination. 遍历两个数组,并在每个步骤上进行新的字符串组合。 After all just print combinations in some way.
毕竟只是以某种方式打印组合。
List<String[]> input = ...;
Set<String> allCombinations = new HashSet<>();
for (int i = 0; i < input.size() - 1; i++) {
for (int j = i + 1; j < input.size(); j++) {
allCombinations.addAll(twoArraysCombinations(input.get(i), input.get(j)));
}
}
// print allCombinations
private static Set<String> twoArraysCombinations(String[] first, String[] second) {
Set<String> result = new HashSet<>();
for (String f : first) {
for (String s : second) {
result.add(f + s);
}
}
return result;
}
Edit: Author changed his question to specify that there are more than 2 lists in an array. 编辑:作者更改了他的问题,以指定一个数组中有两个以上的列表。 This answer works for 2, but it can used to expand to more.
该答案适用于2,但可以用于扩展到更多。 It's still unclear if we wants ALL possible combinations, or just combinations of 2
尚不清楚我们是否需要所有可能的组合,或者仅是2个的组合
Use nested loops to iterate over both ArrayList
s and then concatenate and print the strings. 使用嵌套循环遍历两个
ArrayList
,然后连接并打印字符串。
If both String[]
arrays are in the ArrayList
you should pull them out first with String[] A = yourArrayList.get(0)
and String[] B = yourArrayList.get(1)
如果两个
String[]
数组都在ArrayList
,则应首先使用String[] A = yourArrayList.get(0)
和String[] B = yourArrayList.get(1)
将它们拉出。
for (String aString : A)
for (String bString : B)
System.out.println(aString + bString);
You can use: 您可以使用:
for (String a: list.get(0)) {
for (String b: list.get(1)) {
System.out.println(a + b);
}
}
Where list
is your original List<String[]>
list
是您的原始List<String[]>
Just use 2 for-loops 只需使用2个for循环
String a [] ={"a","b","c"};
String b [] ={"a","b","c"};
ArrayList<String> d = new ArrayList<>();
for (String a1 : a) {
for (String b1 : b) {
d.add(a1 + b1);
}
}
System.out.println(d.toString());
Impressed with the answers so far....Read the question first guys 迄今为止的答案给人留下了深刻的印象。...首先阅读问题
I'll give you an outline of a solution (warning typos?). 我将为您提供解决方案的概述(警告错别字?)。 I'll leave it up to you to decide how to put commas between entries and other little intricacies incase this is a homework question.
如果这是一个家庭作业问题,我将由您决定如何在条目和其他小巧的地方加上逗号。 Another poster showed how to put the items in a list instead of printing them.
另一位海报展示了如何将这些物品放入列表而不是打印它们。 Depending on your exact needs, you may want to merge my example with theirs.
根据您的确切需求,您可能希望将我的示例与他们的示例合并。
List<String[]> stringLists = ....
for(int firstListIndex = 0; firstListIndex < stringLists.size(); firstListIndex++){
String[] primaryList = stringLists.get(firstListIndex);
for(int secondList = firstListIndex + 1; ....){
String[] secondaryList = ....
for(String first : primaryList){
for(String second : secondaryList){
System.out.println(first + second);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.