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.

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but Elliott also answered with nested, too.
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);

1 Comment

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

2 Comments

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

1 Comment

This does not produce the correct result. The output of this code is: diamond Leggings<br> Iron Boots<br> Leather Helmet

Your Answer

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.