Java Reference
In-Depth Information
You can have arrays of objects as well as primitives.
You have the helper class
java.util.Arrays to help you work with arrays. For example,
it provides methods for searching through an array, sorting the array elements, etc.
Using an array in Java has the following disadvantages:
Arrays are fixed in size. You must specify the size at the time of creation. Once created, the
array size cannot be changed. That is, arrays cannot expand or shrink if you need them to.
If you store an element in an array at specific position and later you want to remove it, there is
no way to know that the element at that position was removed.
Compile-time type checking, though an advantage, also becomes a disadvantage. It cannot
store different kinds of values. For example, a reference array of a Car class will store only Car
type objects. A primitive array of double will only store values of double type.
You need to write a lot of code if you want to implement a specific type of collection using an
array. For example, suppose you want to have a collection that should not allow duplicate
values. Of course, you can develop a new class that uses an array to implement your collection.
However, it is a time-consuming task.
The Collections Framework provides all the features provided by arrays, and then some. It provides many other
features that are not provided by arrays. The Collections Framework team has already gone through the pain of
designing, developing, and testing the interfaces and classes that are needed to use different kinds of collections.
All you need to do is to learn those classes and interfaces, and use them in your Java programs.
You need to keep the following points in mind when you learn about collections:
Collections are designed to work only with objects. To work with collections of primitive
types, either you wrap and unwrap your primitive values in wrapper objects or you can take
advantage of the built-in autoboxing features in Java that will wrap and unwrap the primitive
values as needed.
All collection classes in Java are declared generic. That is, you can specify the type of elements
that your collection deals with as the type parameter.
Architecture of the Collection Framework
The Collections Framework consists of three main components:
Interfaces
Implementation Classes
Algorithm Classes
An interface represents a specific type of collection in the framework. There is one interface defined for every
type of collection in the framework; for example, the List interface represents a list, the Set interface represents
a set, the Map interface represents a map, etc. Using an interface to define a collection (rather than a class) has the
following advantages:
Your code, which is written using interfaces, is not tied to any specific implementation.
Classes that implement collections defined by interfaces may be changed without forcing you
to change your code that was written using interfaces.
You can have your own implementation for a collection interface to suit specific needs.
 
Search WWH ::




Custom Search