I am working on my own personal app for minecraft. I would forget what recipe I need, and I would just search, and have it display the recipe.
Now, I have the list and search function with it being alphabetical. Now I am manually adding images, and everything else I need. BUT I think it would be more efficient if I had a array string like this
String test1[] = { "diamond", "Iron", "Leather" };
String test2[] = { "Leggings", "Boots", "Helmet", "Chestplate" }
and in my list view I want the end result to be like this.
Diamond leggings
Diamond boots
Diamond Helmet
Diamond Chestplate
Iron Leggings
...
...
Gold Leggings
...
...
...
What would I need to do to achieve that? I think it would be ineffecient if I did it like this test3.add("Diamond Chestplate") test3.add("Diamond boots") etc.. .. ...
and end up having big list instead where I can combine them.
-
1Try a couple of nested loops. Comment again if you get stuck.Dawood ibn Kareem– Dawood ibn Kareem2014年11月06日 03:32:17 +00:00Commented Nov 6, 2014 at 3:32
4 Answers 4
Use 2 nested for loops to merge the arrays:
String test1[] = {"Diamond", "Iron", "Leather"};
String test2[] = {"Leggings", "Boots", "Helmet", "Chestplate"};
List<String> merged = new ArrayList<String>();
for (String str1 : test1) {
for (String str2 : test2)
merged.add(str1 + " " + str2);
}
System.out.println(merged);
-
Thanks for the answer, but Elliott also answered with nested, too.andyADD– andyADD2014年11月06日 03:53:35 +00:00Commented Nov 6, 2014 at 3:53
If I understand your question, you could do with nested For-Each
Loop(s) like
String test1[] = { "Diamond", "Iron", "Leather" };
String test2[] = { "Leggings", "Boots", "Helmet", "Chestplate" };
List<String> al = new ArrayList<>();
for (String i : test1) {
for (String j : test2) {
StringBuilder sb = new StringBuilder(i);
sb.append(' ').append(j);
al.add(sb.toString());
}
}
System.out.println(al);
-
thank you this is perfect, I will need this in the future and will be able to compile my list with a lot less line of code. Thanks!andyADD– andyADD2014年11月06日 03:52:00 +00:00Commented Nov 6, 2014 at 3:52
I am curious to know that If, I can achieve it in a single loop. How to do it, and finally I have done it.
private static String test1[] = {"Diamond", "Iron", "Leather"};
private static String test2[] = {"Leggings", "Boots", "Helmet", "Chestplate"};
public static void doInSingleLoop() {
int maxLength = test1.length * test2.length;
List<String> al = new ArrayList<String>();
for (int i = 0; i < maxLength; i++) {
String t1 = test1[i / test2.length];
String t2 = test2[i % test2.length];
StringBuilder sb = new StringBuilder(t1);
sb.append(' ').append(t2);
al.add(sb.toString());
}
System.out.println(al);
}
-
1This required a change from
String t1 = test1[i / test1.length];
toString t1 = test1[i / test2.length];
, or it would produce an incorrect result and an ArrayIndexOutOfBoundsException. Edit submitted.howrad– howrad2014年11月06日 19:03:42 +00:00Commented Nov 6, 2014 at 19:03 -
Also, even when using a single loop, it's still an O(N^2) problem, so this answer is probably not any more efficient than a nested loop. But it is an important exercise.howrad– howrad2014年11月06日 19:07:27 +00:00Commented Nov 6, 2014 at 19:07
Check below example.
public static void main(String[] args) {
String firstNameArr[] = { "diamond", "Iron", "Leather" };
String lastNameArr[] = { "Leggings", "Boots", "Helmet"};
List<String> fullNameList = new ArrayList<String>();
for (String firstname : firstNameArr) {
for(String lastName : lastNameArr){
fullNameList.add((firstname+" "+lastName));
}
}
}
-
This does not produce the correct result. The output of this code is: diamond Leggings<br> Iron Boots<br> Leather Helmethowrad– howrad2014年11月06日 18:45:11 +00:00Commented Nov 6, 2014 at 18:45