Typically, a program always creates new objects based on certain conditions that are known to the runtime.
Until then, you don't know the number of objects you want, or even the exact type.
So, to solve this pervasive programming problem, you need to create any number of objects at any time and anywhere:
First, you cannot rely on creating a named reference to hold each object:
MyType areference;
Reason:
Do not know how many such references will actually be needed. Most languages provide an array of ways to save objects (which should be referred to as objects)
However, an array is an effective way to save a set of objects or basic types of data, which, because of its fixed size, is generally very limited.
Solve:
Java uses a class library to provide a fairly complete set of container classes to solve this problem.
List, Set, Queue, Map
Note that these object types are also known as set collection classes, but because Java's class library uses the name collection to refer to a special subset of the class library, it uses "containers" to address lists and so on.
Containers provide a perfect way to save objects and solve an astonishing number of objects. For example, set holds only one object for each value, and map allows you to associate an associative array of objects with some other object.
Java container classes can automatically resize, so there's no need to worry about how big the container should be.
Container classes are the basic tools that can significantly enhance your programming capabilities.
Basic concepts
Purpose of the Java Container Class library: Saving objects
A:collection |
A sequence of independent elements List: The insertion Order must be installed to save the element Set: cannot have duplicate elements Queue: Determines the order in which objects are produced according to the queuing rules (usually in the same order as they are inserted) |
B:map |
A pair of "key-value pairs" objects that allow keys to be used to look up values Arraylsit: Allows numbers to be used to find values, in a sense it associates numbers with objects Mapping table: Allows you to use another object to find an object, called an associative array (because it associates objects with other objects), or "dictionaries" (because you can use a key object to find a value object, as defined by using a word in a dictionary) |
Example:ArrayList is the most basic and reliable container
Create an instance |
|
Add () Insert Object |
|
Get () access to these objects |
The index needs to be used at this point, like an array, but no square brackets are required |
Size () method |
You can know how many elements have been added, so you won't accidentally throw an error because the index is out of bounds |
Java methods for holding objects--containers