O'Reilly logo

Java® Programming 24-Hour Trainer by Yakov Fain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 14

Introduction to Collections

So far you’ve been introduced to only one way of storing a collection of objects — with Java arrays, which are good for storage, but fall short when you need to dynamically add or remove data, or to sort or traverse your collection. There are a number of classes and interfaces in the package java.util that are quite handy when multiple instances of some objects have to be co-located in memory. This lesson will introduce you to several of them.

You can find more collections in the java.util.concurrent package, but those will be reviewed in Lesson 21 after you become familiar with the concept of multi-threading. Together, the collection classes and interfaces located in java.util and java.util.concurrent are often called the Java Collection Framework.

Arrays Revisited

Java collection classes enable the storing of handles of related data in one place. Here a handle is a reference to the location of an object in memory. You were introduced to arrays in Lesson 5: Arrays let you store and access a group of variables of the same type. Let’s go over the steps you follow to declare and populate an array.

First, declare a variable of the type that matches the types of the objects that will be stored in the array, and reserve enough memory to accommodate all objects. For example, to reserve memory enough for storing 10 instances of class Customer you can declare a variable cust, as follows:

   Customer cust[] = new Customers[10];

At this point ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required