Use a Java ArrayList

An ArrayList in Java also keeps track of a list of values, just like a simple Array does. ArrayLists are a little messy to declare but simple enough to use, and much more flexible and a bit safer than plain old Array objects. You can add and delete from the ArrayList as many times as you want; it’s not a fixed length and will grow or shrink as needed.

Here’s an example of an ArrayList that will hold Player objects:

 
List​<Player> myPlayerList = ​new​ ​ArrayList​<Player>();

There’s a lot more gunk in there than we’ve seen up to now. First of all, I’ve called our new list myPlayerList. Note that funny syntax with the angle brackets, < and >. You have to specify the kind of list you’re making (twice, in fact). Java 7 improves ...

Get Learn to Program with Minecraft Plugins, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.