Java [JAVA] please tell me this for loop

  • Thread starter Thread starter uperkurk
  • Start date Start date
  • Tags Tags
    Java Loop
AI Thread Summary
To convert an ArrayList<String> into a single string output in Java, a for loop can be utilized effectively. The user can create a String variable, such as "boxtext", to accumulate the elements of the ArrayList. The recommended approach involves using an enhanced for loop, which simplifies the iteration process. For example, using "for(String str : al)" allows for easy access to each element without the need for explicit iterator management. However, it is important to note that this method is suitable for read-only access; attempting to modify the ArrayList during iteration will lead to a ConcurrentModificationException. For older Java versions (prior to Java 5), traditional for loops or while loops may be necessary, as enhanced for loops are not supported. This discussion highlights the importance of understanding the version of Java being used when implementing these looping constructs.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top