Wednesday, September 8, 2010

Using arraylists properly with android


Late post so ill keep it short.

Android development hinges on being efficent with memory. Declaring objects in a loop is not efficient. If you use a foreach loop with an arraylist, you are creating an iterator each time you run through the loop. Instead, loop through your arraylist using a for loop like this:

for(I =0; I<mylist.size();I++){

       Myobject=mylist.get(I);

...

)

That will prevent memory leaks and keep your game or app running smoothley since the garbage collector will be swift.  See my other post for how to track all memory leaks while developing with android.

No comments:

Post a Comment