Usually know some of the program is always according to the operation conditions to create the object, don’t know the number of the object before then, don’t even know the exact type, in order to solve the general programming problem, need at any time and any place to create any number of objects, so they can’t rely on create a named reference to hold each object, Because you don’t know how many of these references you’ll actually need.

MyType aReference;
Copy the code

In addition to the fixed-size structure of arrays, the Java utility library provides a fairly complete Set of container classes to address this problem, with the basic types being List, Set, Queue, and Map.

Generics and type-safe containers

By using generics, you can specify the types that the container can hold and prevent objects of the wrong type from being placed in the container at compile time. The compiler can prevent you from putting Orange in apples, so it becomes a compile-time error rather than a run-time error. In addition, type conversions are no longer required when an element is taken out of the container.

When you specify a type as a generic parameter, you are not limited to putting objects of that exact type into the container. Upcasting applies to generics just as it applies to other types, so you can add Apple subtypes to containers that are designated to hold Apple objects. Polymorphism does not conflict with generics.

The iterator

To use a container, you must program for the exact type of the container. If you originally coded against a List, but later found that it would be convenient to apply the same code to a Set, the concept of an iterator can be used for this purpose.

The Java Iterator can only move in one direction. This Iterator can only be used:

  • Using the iterator() method requires that the container return an iterator interface and be ready to return the first element of the sequence.
  • Use next() to get the next element in the sequence.
  • Use hasNext() to check if there are any more elements in the sequence.
  • Use remove() to remove the element recently returned by the iterator. This requires that we first call next(), which internally resets the cursor so we can continue iterating.

ListIterator is a more powerful subtype of Iterator that can only be accessed by various List classes. Although the Iterator can only move forward, ListIterator can move both ways. It can also produce indexes of the elements before and after the current position the iterator points to in the list, and it can use the set() method to replace the last element it visited.

So far, foreach has been used primarily for arrays, but it can also be applied to any Collection object. Java introduced the Iterable interface, which contains an Iterator () method that produces iterators. If you create any classes that implement Iterable, You can use it in foreach syntax (arrays are a special case).

Java containers have a protection mechanism that prevents multiple processes from modifying the contents of the same container at the same time. If you iterate through a container and another process steps in and inserts, deletes, or modizes an object in the container, So Java using rapid error (fail – fast) mechanism, immediately throw ConcurrentModificaitonException anomalies.

public class FailFast { public static void main(Stirng[] args) { Collection<String> c = new ArrayList<String>(); Iterator<String> it = c.iterator(); c.add(""An object); try { String s = it.next(); } catch(ConcurrentModificaitonException e) { System.out.println(e); }}}Copy the code