|
Arrays and CollectionsHere is a practical (rather than theoretical) comparison of how different programming languages use arrays and Collections. I describe the operations a programmer wants to do (adding, updating, sorting) rather than repeating SDK lists of classes, algorithms, wrappers, etc. Let me know if this helps you make more appropriate selection of collection classes/interfaces to suit the behaviors you want.
|
|
|
JavaScript ArraysJavScript 1.0 authors had to create their own makeArray() function. Note the need in JavaScript to explicity specify JavaScript 1.1 has an array object. VIDEO: 6:00 into JavaScript Arrays in WinJS within Windows 8 include functions to push, pop, slice, splice, join, concat, map, filter, some, every, forEach, reduce, sort, reverse (like Lync).
|
The Java Collections Framework
A Java collection (implementation of package java.util new to Java 1.2), can store data of different types in a single structure and be sized dynamically (allowed to grow or shrink). It's a refinement of the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy.
|
Defensive Array Programming
To protect Java array accesses, precede them with assert statement:
i = array[index];
|
Array Manipulations
n=args.length; for( int i=0; i<n; i++ ) { System.out.println( "Parm "+ i +"="+ args[i] ); } An index points to the value in each element within an array. Its use of a sequential index means arrays cannot contain duplicates. The first element is selected by subscript 0 (zero). The array length property -- the number of elements within an array -- is established when the array is created (at runtime). Arrays are not growable after initialization. Array elements are selected by enclosing an integer expression in brackets ([]). Unlike C, if an array index refers outside the specified range, Java throws a ArrayIndexOutOfBoundsException rather than allow buffer overrun conditions that can be exploited by hackers. Arrays can make use of collection methods for sorting and expansion when converted to a list collection List listX=java.util.Arrays.asList( arrayY );
|
Marcus Green's notes on the SCJP exam object 1 Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization.
|
Each additional level in the subclass makes more methods available. For example, when an ArrayList is instantiated by extending AbstractList and implementing List, it inherits methods of the Collection interface because AbstractList extends the AbstractCollection interface, which provides the toString() method. Abstract classes allow a collection to implement optional methods. To make a collection modifiable, simply override the set( object ) method. To make a collection grow, override the add( object ) and remove( int index) methods. New elements are added immediately prior to the implicit cursor. After adding an element, calling previous() returns the new element and calling next() returns what would have been the next element prior to the add operation. iterator() and size() methods are not in the AbstractCollection class because they need to be relevant to each structure. To make a twoi-dimensional array, define an array within an array, such as:
or String[] [] FirstName = new String[14] []; |
Sorting/OrderingThere are several ways to order and re-order objects. Primitive String and Integer classes can implement the java.lang.Comparable interface to use a natural sorting order using the lexicographical compareTo() method, which places numbers before alpha letters and upper case A-Z letters before all lowercase a-z letters. Linked list Abstract classes allow a Collection to be sorted by being instantiated as another type:
Set set = new HashSet(); ... Set sortedSet = new TreeSet(set); To define objects (such as dates) can be inserted in reverse order:
Comparator comparator = Collections.reverseOrder(); Set reverseSet = new TreeSet(comparator); reverseSet.add("A"); For locale-specific ordering, use Collator with CollationKey. The Java Collections Framework provides standard methods for working on all elements in the entire collection all at once:
|
Collections Interfaces & Classes
There are several implementations of the Collection concept.
BitSet, an array of boolean bits, is not a set and not part of the Collections framework. |
Collections Implementation
|
java.util.Collection Framework
The Java collection framework enables polymorphic processing -- manipulation of one collection type through a different collection type, regardless of the collection's internal implementation. The different implementations of Collection can all use the same methods defined by the Collection interface because the parent Collection interface defines the names of generic methods to:
The Collection superclass is abstract because it is not meant to be implemented, but its subclasses are implemented. An abstract class cannot be instantiated. Subclassing AbstractCollection directly implements a bag -- an unordered collection that allows duplicate elements. To provide functionality for ordering elements, sorting, handling growth, and handling duplicates, AbstractCollection is subclassed by AbstractList, AbstractSet, and AbstractMap. Collection subclasses are concrete (not abstract) so that an implementation does not have to override every method of the abstract class. To implement a List, only size() and get() methods need to override what is inherited from subclassing AbstractList. Functionality to modify a List are optionally defined by overriding set(), add(), and remove() methods. Similarly, to extend an unmodifiable AbstractMap, only the entrySet() method needs to be overridden and the Map.Entry implemented. For a modifiable Map, override add(), remove(), put(), and support remove() of the iterator returned by the entrySet().Iterator(). The Enumeration interface provides two methods for stepping through an indexed set of objects: The Iterator interface enables next(object) and boolean query hasNext(). Its remove() void method delete objects when an enumerator does not. ListIterator extends the Iterator interface by providing methods for iterating through the elements of a list bi directionally: previous(), hasPrevious(), plus previousIndex() and nextIndex() iterators for the concrete collection implementations are fail-fast. That means that if you are using an Iterator to traverse a collection while underlying collection is being modified by another thread, then the Iterator fails immediately by throwing a ConcurrentModificationException (another RuntimeException). That means the next time an Iterator method is called, and the underlying collection has been modified, the ConcurrentModificationException exception gets thrown. The Comparator interface provides the compare() method for comparing the elements of a collection. Method equals() returns true if two sets are the same size and contain the same elements in the same order.
| Sun's Java Tutorial on Arrays Introduction to the Collections Framework by John Zukowski of the MageLang Institute The hierarchy of classes of the java.lang.Object package are: Dictionary-Stores key-value pairs.
|
Related:
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |