Using for loop to get all elements in an ArrayList?

Hi,

I'm trying to write a method that will use a for loop to iterate through all the positions in an ArrayList and add them to a String. I then want to return that String outside the for loop. Whenever I do this my for loop is ignored and what I initialised the String with is what is returned instead :S

[code]
String wordPos = null;
for (int i = 0; i < position.size() - 1; i++)
{
wordPos = wordPos + position.get(i);
}

return wordPos;
[/code]

I hope you can tell me what is going wrong or a better way of doing this.

Thanks

Comments

  • Maybe you should try to declarate
    wordPos = "";
    And are you sure that you made your ArrayList a "String type".
    I hope this helps you?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion