If you have done a lot of java programs, you may be familiar with java Collection classes, such as Vector and ArrayList. You can create a set and add elements to it:
-
- List lst =NewArrayList ();
-
- Lst. add (New Integer(37 ));
In this special example, an Integer value 37 is used to construct an Integer encapsulation class object, which is then added to the List.
This simple example shows the basics of a set-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 the Object, String, Float, and Runnable types. The Collection class cannot be used for a list of original data types, such as an integer array.
If you use arrays of the original type in your program, how do you manipulate them? This tip shows you some of the technologies you can use.
The first technology is sorting. The java. util. Arrays class contains a set of class methods for sorting and searching Arrays. For example:
-
- ImportJava. util.Arrays;
-
- Public ClassArrayDemo1 {
- Public Static VoidMain (StringArgs []) {
- IntVec [] = {37, 47, 23,-5, 19, 56 };
- Arrays. sort (vec );
- For(IntI = 0; I <vec.Length; I ++ ){
- System. Out. println (vec [I]);
- }
- }
- }
This demo initializes an integer array and then calls Arrays. sort to sort the array in ascending order.
Similarly, you can perform a binary search on the sorted array:
-
- ImportJava. util.Arrays;
-
- Public ClassArrayDemo2 {
- Public Static VoidMain (StringArgs []) {
- IntVec [] = {-5, 19, 23, 37, 47, 56 };
- IntSlot = Arrays. binarySearch (vec, 35 );
- Slot =-(slot + 1 );
- System. Out. println ("insertion point =" + slot );
- }
- }
This program has a subtle concept. If the binary search fails, it will return:
-(Insertion point)-1
This demo calls the search method with parameter 35, and the parameter does not exist in the array. the return value of the method is-4. If this value is added and its negative number is obtained, 3 is obtained, this is the position where 35 should be inserted into the array. In other words, values-5, 19, and 23 occupy 0, 1, and 2 in the array. Therefore, the value 35 should be at the index 3 position, while the values 37, 47, and 56 are postponed. The search method does not actually insert data, but just specifies where to insert data.
In addition to sorting and searching, what can we do for the original array? Another useful technique is to convert an original array to an equivalent array of object types. Each corresponding element uses their encapsulate class. For example, in an encapsulated array, 37 becomes Integer (37 ).
-
- ImportJava. util.Arrays;
- ImportJava. lang. reflect.Array;
- Public ClassArrayDemo3 {
- // If input is a single-dimension primitive array,
- // Return a new array consisting of wrapped elements,
- // Else just return input argument
- Public Static ObjectToArray (ObjectVec ){
- // If null, return
- If(Vec =Null){
- ReturnVec;
- }
- // If not an array or elements not primitive, return
- ClassCls = vec. getClass ();
- If(! Cls. isArray ()){
- ReturnVec;
- }
- If(! Cls. getComponentType (). isPrimitive ()){
- ReturnVec;
- }
- & Nbs