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 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 lookup 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