I am trying to create a basic for loop that adds the elements of a temporary List to the main ArrayList. This causes 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 debugging showed that tempFavList.size() is equal to 2 before the for loop is called, but goes to infinity when the for loop is called (well at least to +500,000 before the App crashes). The list tempFavList is a List that is pulled from a Parse database using the code tempFavList = currentUser.getList("favourites");
I am fairly confused why the for size of the temporary List is increasing once the for loop is called, as I am not adding any items in the for loop. Any help would be greatly appreciated
You can try:
final int tempSize = tempFavList.size();
for (int i = 0; i < tempSize; i++){
....
}
Ok so here is what I think you are doing 95% of the code I post here is probably what you have
import java.util.ArrayList;
import java.util.List;
public class Temp {
static ArrayList<String> favourites = new ArrayList<String>();
public static void main(String[] args) {
List<String> myGuiltyList= CurrentUser.getList("favourites");
for (int i = 0; i < myGuiltyList.size(); i++) {
System.out.println("myInnocentList size = " + favourites.size());
favourites.add(myGuiltyList.get(i));
if (myGuiltyList.size() == 1000) {
break;
}
}
}
private static class CurrentUser {
public static List<String> getList(String listName) {
favourites.add("1");
favourites.add("2");
favourites.add("3");
if (listName.equals("favourites")) {
return favourites;
}
else return null;
}
}
}
The problem is that the static list you have named favourites is actually the SAME list you get when you call the CurrentUser.getList("favourites"); line.
So they are actually the same list and you add element to the same list and the size of the list increases with every loop and the loop will never stop cos the size of the list will never be smaller than i cos they increase with the same ratio
:D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With