0

I have three Arraylists like this

List<String> list1 = new ArrayList<String>();    
List<String> list2 = new ArrayList<String>();    
List<String> finallist = new ArrayList<String>();   

list1 contains items "a","b" list2 contains items 1 , 2, 3 how to make a finallist that will contain "a1" "a2" "a3" "b1" "b2" "b3"

4
  • 4
    From where Letter 'C' appeared? Commented Jul 14, 2012 at 16:40
  • also, "final" is a reserved word in Java. You can't have it as a variable name. Commented Jul 14, 2012 at 16:47
  • you've said a1, a2, a3, but not 1a, 1b, 1c, do you only want combinations and not permutations? Commented Jul 14, 2012 at 17:06
  • Also, where did the "c" come from to make "c1", etc? Commented Jul 14, 2012 at 20:43

1 Answer 1

5

Assuming that the lists look like this:

    List<String> list1 = Arrays.asList("a", "b", "c");
    List<String> list2 = Arrays.asList("1", "2", "3");

then:

    List<String> finalList = new ArrayList<String>();
    for (String letter: list1) {
        for (String number: list2) {
            finalList.add(letter + number);
        }
    }

    System.out.println(finalList);
Sign up to request clarification or add additional context in comments.

Comments

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.