[JAVA] please tell me this for loop

  • Java
  • Thread starter uperkurk
  • Start date
  • Tags
    Java Loop
In summary: This one is:In summary, the for loop in the example code will have no problems removing objects from the ArrayList while the while loop will not.
  • #1
uperkurk
167
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
  • #3
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:
  • #4
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!
 
  • #5
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.
 

Related to [JAVA] please tell me this for loop

1. What is a for loop in Java?

A for loop in Java is a control flow statement that allows you to execute a set of code repeatedly for a specific number of times. It is typically used when you know the exact number of iterations needed for a task.

2. How do you write a for loop in Java?

The syntax for a for loop in Java is as follows:
for (initialization; termination condition; increment/decrement) {
  // code to be executed
}

3. What is the purpose of the initialization, termination condition, and increment/decrement in a for loop?

The initialization is where you declare and initialize a variable that will control the number of iterations. The termination condition is a boolean expression that is evaluated before each iteration, and if it is true, the loop will continue. The increment/decrement is used to update the control variable after each iteration.

4. Can you provide an example of a for loop in Java?

Here is an example of a for loop that prints the numbers 1 to 10:
for (int i = 1; i <= 10; i++) {
  System.out.println(i);
}
Output:
1
2
3
4
5
6
7
8
9
10

5. When should I use a for loop in Java?

A for loop is useful when you need to iterate through a collection of items or perform a repetitive task a specific number of times. It is also commonly used when working with arrays or when you need to perform a certain action until a condition is met.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
910
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top