406 Not Acceptable

ArrayList in J2ME and generics

by Jim on Apr.01, 2010, under Java, j2me

As you may, or may not know, J2ME does not support the ArrayList functionality of standard Java. In addition, as J2ME is based on Java 1.3 it does not feature generics. This means as a developer you are stuck with a choice of using arrays or vectors. Now arrays are fast, but you are stuck with the initial size and a number of other issues. These are things you really do not want to be messing around with, unless speed is of the highest priority.

Whilst Vectors are dynamic, they bring about one annoying issue – you have to cast each entry back from an Object type to its original type on retrieval. This takes up a lot of processor cycles, and on phones processor cycles are a limited commodity. Secondly, you will have to use the Vector enumerator to iterate through your vectors. This is very processor intensive, in NaStar I found this was using around 30% of my cycles! Finally Vectors are synchronised, this means they are thread safe. Now a synchronised collection is much slower than an unsynchonised one; plus, you will most likely not need the safety benefits synchronisation brings. As you will usually be using one thread for acessing your collections in a game (in the main game loop.)

Now the J2ME Polish project gives you the chance to use some Java 1.5 features, most importantly generics are included. Second, an arraylist implementation is included, which uses generics. However, many people have had issues with integrating this plugin to their chosen IDE. I have worked out some fixes for Netbeans J2ME Polish integration, but when coding my project decided against using the tools.

So you now find yourself stuck between no generics, slow vectors, or fixing (and maintaining) a plugin.

If all you want is access to ArrayLists or generics, your best bet is to strip the ArrayList code from J2ME Polish and create your own lists. I guess you could simply find an implementation of ArrayList floating around on the web, but the J2ME Polish code is very good. You will then simply create a number of new ArrayList classes, such as SpriteList, EnemyList, et al. There will be two other classes that the ArrayList depends on, you should also clone these for each implementation; these will let you use for each loops! Let each list only deal with one defined type. Now you will no longer have to deal with Vectors and their iterators and will find your game runs a lot faster! I found around 20 – 60% of processor time was dealing with Iterators; whilst ArrayLists cut this down to under 10%. For my project this was the best performance tweak available.

Before anyone asks, no you can’t play my game! Sadly the rights are held by the University of Birmingham :( Whilst I doubt they care much about a J2Me game!

Related posts:

  1. Fixing J2ME Polish Integration With Netbeans 6
  2. Sorry about the lack of J2ME…
  3. Distinction!
  4. Collision detection in J2ME
  5. Breadth First Tree Traversal in Java

:, , , ,

Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...