This article has a collection value, just uses to use the beginner.
Transfer from: http://www.javaresearch.org/article/showArticle.jsp? Column = 1 & thread = 2994
If you have done a lot of Java programs, you may be familiar with the Java collection class, such as Vector and ArrayList. You can create a collection and add an element to it:
List lst = new arraylist (); lst.add (new integer (37));
In this special example, a integer value 37 is used to construct an Integer package class, and then that object is added to the list.
This simple example shows a basis for the collection - they are used to manipulate a column of objects, each of which is a class or interface type. Therefore, an ArrayList can contain objects of Object, String, Float, and Runnable types. The set class cannot be used for the list of raw data types, such as integer arrays.
If you use the original type of original type in your program, how do you manipulate them? This trick gives you a few techniques you can use.
The first technology is sorted. The Java.util.Arays class contains a class method for sorting and looking for arrays, such as:
Import java.util.arrays; public class arraydemo1 {public static void main (string args []) {INT VEC [] = {37, 47, 23, -5, 19, 56}; arrays.sort (vec); for (INT i = 0; i This demo initializes an integer array and then calls Arrays.Sort ascending order to sort the array. Similarly, you can perform dichotomal search on arrays of row. Import java.util.arrays; public class arraydemo2 {public static void main (string args []) {int VEC [] = {-5, 19, 23, 37, 47, 56}; int slot = arrays.binarysearch (VEC , 35); slot = - (slot 1); system.out.println ("INSERTION POINT =" slot);}} This program has a subtle concept, if the two-point method finds a failed it will return: - (Insertion Point) - 1 This demo program calls the lookup method in parameter 35, and that parameter does not exist in the array, the method returns value -4, if this value is added again and again to get 3, this is 35 should be inserted into the number in the array, in other words The position occupied by the value -5, 19 and 23 in the array is 0, 1, and 2. Therefore, the value 35 should be in the position of the index 3, and 37, 47, and 56 are delayed. The search method does not perform the actual insertion operation but is merely indicating where to insert. In addition to sorting and looking, what can we do for the original type of array? Another useful technology is to convert an original array to an equivalent object type array. Each corresponding element uses their package, for example in the package array, 37 becomes Integer (37). import java.util.Arrays; import java.lang.reflect.Array; public class ArrayDemo3 {// if input is a single-dimension primitive array, // return a new array consisting of wrapped elements, // else just return input argument Public Static Object ToArray (Object VEC) {// if Null, Return IF (VEC == Null) {Return Vec;} // if not an array or elements not primitive, return class cls = vec.getClass (); if (); ! cls.isArray ()) {Return Vec;} if (! CLS.GetComponentType (). isprimitive ()) {Return Vec;} // Get Array Length And Create Object Output Array Int Length = array.getlength (VEC); Object newvec [] = new object [length]; // wrap and copy elements for (int i = 0; i The parameter "toArray" is an Object object (the array can be assigned to an Object reference). If the parameter is NULL or the representative is not the original type array, then this method simply returns the parameter value. The java.lang.class tool class is used to determine if the parameter is an array and get the type of the underlayer element of the array. Once you have finished these checks, use the Java.lang.Reflect.Array tool class to get the length of the original array and get a single element of the array. Each element obtained by array.get is returned to the package class, such as Integer or Double. The final example is based on the previous one and showing you how to use the collection features on an array. This assumes that you already have an object array. Import java.util.arrays; import java.util.list; public class arraydemo4 {public static void main (string args []) {Object vec [] = {new integer (37), new integer (47)}; list LST = Arrays.aslist (vec); lst.set (1, new integer (57)); for (int i = 0; i In this program, VEC is an object array, contains Integer (37) and Integer (47), and then Arrays.aslist is called. It returns a collection (List interface type) that uses arrays as a set of background storage. In other words, the set type of ArrayList has some kind of storage type to store the collection element within it. In this example, the storage type used is an array that is passed to arrays.aslist as a parameter. This means that the change in the collection method will be reflected to an array of underlying. Elements 1 in the set of collections results in the underlying array changes, and the output of the program is: 37 57 So if you have an object array, you can use the collection characteristics above it, and the array itself is stored as the underlying. We can also convert the collection into an object array, for example: Object vec [] = lst.toArray ();