Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Oracel Java Tutorial on Collections
ArrayList
-
You can point an element of
ArrayorArrayListtonull, but remember that anullreference cannot invoke any method. For example, if you have an objectsetofArrayList<ArrayList<Point>>whose first element isnull. Then you cannot add points (objects of Point) to the first element (which is of typeArrayList<Point>) ofsetby invokingset.get(0).add. To achieve the goal, you have to first create a new object ofArrayList<Point>, point the first element ofsetto it, and then add points by invokingset.get(0).add. -
Elements added into an
ArrayListor returned bygetfrom an ArrayList are by reference. -
To insert an element into an
ArrayListwith sizen(using theaddmethod), the legal range of index is0ton. Theaddmethod without specifying location add the element to the end of theArrayList. -
An
ArrayorArrayListin Java is essentially a sequence of addresses. Since an address can point to anything,ArrayorArrayListin Java can be used as container for anything. Specially, you can use an array as a container forArrayListsand you can also use an ArrayList as a container forArrays. For exampleArrayList<double[]> list;and :::java ArrayList
[] array; are both legitimate in Java.
Capacity
-
The
clearmethod ofArrayListdoes not affect its capacity. -
The capacity of an
ArrayListcan be 0. -
You might be curious that why the methods
boolean add(E e)andvoid add(int index, E e)have different signatures. The reason is thatboolean add(E e)is inherited from theCollectioninterface. For other collections such as set and map, etc., it is beneficial to know whether an elements to be added to it already exists or not. The return value (indicating whether the ...) is then very helpful. For the list specific methodvoid add(int index, E e), there is no need to define the signature as boolean because a list does not force uniqueness and whether an element will be added has nothing to do with whether an element of the same value already exists in the list or not.
Array
-
The
Arraysclass contains many userful static methods for array operations, but generally speaing, they can only be applied to 1-d array directly. To applied to multi-dimensional arrays, you have to iterate into the array until you have 1-d arrays. For example, if you want to compare whether two 1-d integer arrays are equal or not, you can use the staticequalsin theArrays. To compare whether two 2-d interger arrays are equal or not, you can compare whether the two arrays have the same length, and whether each corresponding elements (which are 1-d arrays) of the two arrays are equal (using the staticArrays.equalsmethod). ApplyingArrays.equalson a 2-d intger arrays directly compares whether the two 2-d arrays contain same references to 1-d arrays. The staticArrays.equalsmethod is overloaded for double arrays, but it is no a good idea to compare whether two double arrays are equal usingArrays.equalsbecause it does not allow an error tolerance. -
A 2-d array in java is just an array of arrays, so even if you define
int[][] a = new int[2][3];you can still point
a[i]to another 1-d array. This means that you can change the length of each element of the 2-d arrayawhenever you want. -
To copy arrays quicly, you can use
System.arraycopywhich is implemented in native code. It is preferred to other ways such asclone, user-defined method and so on. Notice thatSystem.arraycopyperforms shallow copys for arrays of objects, i.e., only references of objects will be copied. If you want to make a deep copy of an array of objects, you have to do it by yourself. -
A container in Java can be initialized using curly braces (similar to the universal initializer list in C++11), and in this case you do not have to specified the dimensions of the container. To initialized all elements of a 1-dimensional array to a single value, you can the static
Arrays.fillmethod. Note that arrays in Java are automatically initialized to default values, so you need not to initialize an array if the default value is what you want.