0

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.

Rajesh
10.4k16 gold badges47 silver badges65 bronze badges
asked Nov 6, 2014 at 3:29
1
  • 1
    Try a couple of nested loops. Comment again if you get stuck. Commented Nov 6, 2014 at 3:32

4 Answers 4

2

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);
howrad
1,0862 gold badges13 silver badges32 bronze badges
answered Nov 6, 2014 at 3:52
1
  • Thanks for the answer, but Elliott also answered with nested, too. Commented Nov 6, 2014 at 3:53
1

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);
answered Nov 6, 2014 at 3:35
1
  • 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! Commented Nov 6, 2014 at 3:52
1

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);
 }
howrad
1,0862 gold badges13 silver badges32 bronze badges
answered Nov 6, 2014 at 4:49
2
  • 1
    This required a change from String t1 = test1[i / test1.length]; to String t1 = test1[i / test2.length];, or it would produce an incorrect result and an ArrayIndexOutOfBoundsException. Edit submitted. Commented 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. Commented Nov 6, 2014 at 19:07
-1

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));
 	} 
 } 
}

answered Nov 6, 2014 at 4:15
1
  • This does not produce the correct result. The output of this code is: diamond Leggings<br> Iron Boots<br> Leather Helmet Commented Nov 6, 2014 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.