The Java for loop never ends

The Java for loop never ends … here is a solution to the problem.

The Java for loop never ends

I’m trying to create a basic for loop that adds elements of a temporary list to the main ArrayList. This caused my Android app to crash repeatedly.

    for (int i = 0; i<tempFavList.size(); i++){
        Log.v("MyApp",Integer.toString(tempFavList.size()));
        favourites.add(tempFavList.get(i).toString());
    }

Some debugs show tempFavList.size() to equal 2 before calling the for loop, but becomes infinity when the for loop is called (at least +500,000 crashes before the app). The list tempFavList is the use of the code tempFavList = currentUser.getList("favourites");

A list extracted from the Parse database

I’m confused as to why the size of the temporary list increases after calling the for loop because I didn’t add any items in the for loop. Any help would be appreciated

Solution

You can try:

final int tempSize = tempFavList.size();

for (int i = 0; i < tempSize; i++){
    ....
}    

Related Problems and Solutions