0

I have a string like this

String[] tokens = {"Word0", "Word1","Word3"};

and then I have an arraylist like this

List<String> a = new ArrayList();
a.add("This will not change");

What I want to do is to be able to create a new arraylist ,b ,that puts these two together such that: Element 0 of b is "Word0 This will not change" Element 1 of b is "Word0 Word1 This will not change" Element 3 of b is "Word0 Word1 Word3 This will not change"

I played around with for loops and tried to somehow concatenate the strings but I couldn't figure it out.

3
  • 1
    "I played around with for loops and tried to somehow concatenate the strings but I couldn't figure it out." -- consider showing us your attempts. It will help us understand what you may be doing wrong, and gain you a little bit of respect for showing your efforts. Commented Nov 8, 2014 at 0:40
  • Note that this looks for all the world like you're asking us to solve a homework assignment for you. Commented Nov 8, 2014 at 0:41
  • 1
    Specifically one that appears like it should be solved recursively. Also, please change new ArrayList(); to new ArrayList<>();; raw types are so last millennium. Commented Nov 8, 2014 at 0:43

1 Answer 1

1

Something like this:

String[] tokens = {"Word0","Word1","Word2"};
ArrayList<String> a = new ArrayList<String>();
a.add("This will not change");

ArrayList<String> b = new ArrayList<String>();
for (int i = 0; i < tokens.length; i++)
{
    String add = "";
    for (int j = 0; j <= i; j++)
    {
        add += tokens[j];
        add += " ";
    }
    b.add( (add + a.get(0)) );
}

This is tested and works.

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.