Sometimes you need to use an ArrayList. It can be when you’re lazy or when you want flexibility. Eventually, you’ll want to get away from the ArrayList and use your stores objects as standard object array. Doing this isn’t very complicated but it is slightly hidden.
We’re going to pretend for a moment that we have a class called Card and I’m putting a few cards into an ArrayList. After that, I’ll extract the objects from the ArrayList as a simple array.
ArrayList<Card> cards = new ArrayList<Card>(); cards.add(new Card()); cards.add(new Card()); cards.add(new Card()); // Now to get a standard object array that maintains the object type so there is no need for casting. Card c = cards.toArray( new Card[cards.size] );
The c variable will contain the new standard object array. Java should have a method that does this in a more generic sense.