[JAVA] please tell me this for loop

  • Context: Java 
  • Thread starter Thread starter uperkurk
  • Start date Start date
  • Tags Tags
    Java Loop
Click For Summary

Discussion Overview

The discussion revolves around how to convert an ArrayList of strings into a single string output in Java, specifically focusing on the construction of a for loop to achieve this. The context includes coding examples and considerations for different Java versions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant requests help in writing a for loop to convert an ArrayList into a space-separated string.
  • Another participant suggests using a String variable to accumulate the results and mentions alternatives like StringBuffers or StringBuilders for efficiency.
  • A participant provides an example of a collections-based for loop, highlighting its ease of use for read-only access to the ArrayList.
  • There is a reiteration of the collections-based for loop example, with an emphasis on its advantages and potential issues when modifying the list during iteration.
  • One participant notes that the collections-based for loop is only available in Java 5 and later, which may be a limitation for those maintaining older Java code.

Areas of Agreement / Disagreement

Participants generally agree on the utility of the collections-based for loop for read-only access but acknowledge the limitations regarding Java version compatibility. There is no consensus on a single best approach, as multiple methods are discussed.

Contextual Notes

Some participants mention the potential for ConcurrentModificationException when modifying the ArrayList during iteration, and the discussion includes considerations for different Java versions affecting the applicability of certain loop constructs.

Who May Find This Useful

This discussion may be useful for Java programmers seeking to manipulate ArrayLists, particularly those interested in string manipulation and loop constructs in different Java versions.

uperkurk
Messages
167
Reaction score
0
Hey guys, I have an
Code:
ArrayList<String> al = ArrayList<String>();
and I have a textbox, the textbox allows a user to enter a word and that word will be added into the ArrayList.

When I print the list out it looks like this

Code:
[Cat, Dog, Mouse]
can you please write out the for loop that I need in order to change the ArrayList into a string? I want the output to look like this

Code:
Cat Dog Mouse

Please don't say go read this or read that, I've googled for loops but I can't find one that will do what I want.

Thanks.
 
Technology news on Phys.org
The collections-based for loop does almost the same thing, just behind the scenes (it uses and iterator in its implementation), and is a little easier to code:

Code:
ArrayList<String> al = new ArrayList<String>();
al.add("cat");
al.add("dog");
al.add("mouse");

for(String str : al) {
    System.out.println(str + " ");
}

For read-only access to your ArrayList, this is the way to go. You will run into problems if you try to remove objects from your ArrayList in such a loop, however (you will get a ConcurrentModificationException), whereas the while loop in jedishrfu's example will have no such problems.
 
Last edited:
gabbagabbahey said:
The collections-based for loop does almost the same thing, just behind the scenes (it uses and iterator in its implementation), and is a little easier to code:

Code:
ArrayList<String> al = new ArrayList<String>();
al.add("cat");
al.add("dog");
al.add("mouse");

for(String str : al) {
    System.out.println(str + " ");
}

For read-only access to your ArrayList, this is the way to go. You will run into problems if you try to remove objects from your ArrayList in such a loop, however (you will get a ConcurrentModificationException), whereas the while loop in jedishrfu's example will have no such problems.


Exactly what I was looking for thank you so much! I had the exact same thing but I was using

for(str : al) instead of for(string str : al)

Cheers bro!
 
With respect to the collections based for loop just be aware that it is in java 5 and beyond. Sometimes programmers are maintaining java 1.4 code or earlier and collections based for loops aren't supported.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K