0

I'm trying to iterate through an ArrayList and print each element in it but it only outputs the first element in the ArrayList. Then there's an infinite loop and it keeps printing out the first element.

ArrayList<String> startTime = new ArrayList<String>();

for (int i = 0; i < startTime.size(); i++) {
    String getTime = startTime.get(i);
    getTime = convertTime(getTime);
    startTime.add(i, getTime);
    System.out.println(startTime.get(i));
}
3
  • 2
    That's because you are calling startTime.add(i, getTime); in the loop - you are adding an element to the list in each iteration of the loop. Why did you add that line? Remove it. Commented Dec 16, 2016 at 14:36
  • Given the code here, I don't know why you would expect anything different, you are accessing the arraylist, then adding to it. Hence, infinity. Commented Dec 16, 2016 at 14:37
  • This is because you are getting data, overwriting it, getting data, overwriting it etc again and again upto infinity. There is no end. Maybe you can put an if else condition when you reach a certain benchmark and break there. Commented Dec 16, 2016 at 14:42

4 Answers 4

1

When you do startTime.add(i, getTime) you are adding an element in the i position. That means in your next iteration, you are going to have an extra element in your list. When you increment the counter and check startTime.size(), it's always going to be true. Hence your infinite loop.

As a suggestion, if you want to add your getTime element, you might want to use some sort of auxiliary structure, like another List.

Sign up to request clarification or add additional context in comments.

Comments

1

Definitly use advanced for-loops:

ArrayList<String> startTime = new ArrayList<String>();
for(String aStartTime: startTime){
  // do something
}

3 Comments

That may solve his looping problem, but it isn't going to solve the desired update behavior the op is clearly looking for.
Hi @DejaVuSansMono, thank you for your feedback. I have not understood what the op tries to achieve. The code does not make sense for me and the text could be more expressive.
That's fair. OP didn't do a good job explaining the desired behavior. I may be interpreting it wrong myself.
0

Try using this for-loop:

for(int i=0,n=startTime.size();i<n;i++){
    //your for-loop code here  

}

When you were adding elements to startTime, it was increasing its size. Therefore, it upped the boundary that i had to meet infinitely. In my example, n will be set to startTime's size at the beginning of the loop and won't change as the loop executes.

Comments

0

You can try doing the loop backward (from the last element to the first element ) such as:

// assume that I have ArrayList variable named *list*

for(int i = list.size() - 1 ; i >= 0 ; i--){
    list.add(//something);
}

this code won't give you an infinite loop since the loop control variable never going to change ( i >= 0 ) even though your list size keeps changing. The lower bound will always be 0 and the i will decrease towards 0

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.