The application examples are as follows:

package cn.edu.ujn.offersword;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
 
public class C5_33_SortArrayForMinNumber {
 
/** * @date 2016-09-15 * @number 04 * @author SHQ * Enter an array of positive integers. Concatenate all the digits in the array to form a number. Print the smallest number that can be concatenated. * for example, if you enter the array {3,32,321}, the smallest number that can be printed is 321323. * The idea is to implement a custom comparator in collection.sort (). If Mn >nm, nm meets the requirements. * Time complexity O(nlogn); * Space complexity O(2); * /
public static void main(String[] args) {
int[] numbers = {3.5.1.4.2};
System.out.println(PrintMinNumber(numbers));
}
    private static String PrintMinNumber(int [] numbers) {
    	if(numbers == null || numbers.length == 0) {return null;
    	}
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	for(int i = 0; i < numbers.length; i++){
    	list.add(numbers[i]);
    	}
    	// Ascending (custom comparator)
    	Collections.sort(list.new Comparator<Integer>() {
            @Override
            public int compare(Integer str1, Integer str2) {// Implement the methods in the interface
                return (str1+""+str2).compareTo(str2+""+str1); }}); StringBuffer sb =new StringBuffer();
    	for(int str : list){
    	sb.append(str);
    	}
    	returnsb.toString(); }}Copy the code

Note the sort on

Arrays.sort has an overloaded form: sort(T[] a, Comparator
c), this method argument is written using generics, which we haven’t talked about yet. Sort (Object[] a, Comparator c). Sort (Object[] a, Comparator c). There are two methods defined in the Comparator interface: Compare (Object o1, Object O2) and equals. So when we implement the Comparator interface, we just override compare. You don’t need to override equals. Overriding equals(Object) is described in the Comparator interface: “Note that it is always safe not to override object.equals (Object). However, in some cases, overriding this method can improve performance by allowing the program to determine whether the same sort is being enforced by two different comparators.” . We only need to know the first sentence, that is, we don’t have to worry about how to implement equals, because even if we don’t show that we implement equals, but use the equals method of the Object class, the code is still safe.

The sort algorithm is a modified merge sort algorithm (where the merge is ignored if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm provides guaranteed n log(n) performance. Specifies that the list must be modifiable, but not necessarily resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list of each element at the corresponding position in the reset array. This avoids n2 log(n) performance due to trying to sort the list of links in place.

Parameters:

List – The list to sort.

C – comparator to determine list order. The null value indicates the natural order of elements that should be used.

Throws:

ClassCastException – If the list contains elements that cannot be compared with each other using the specified comparator.

UnsupportedOperationException – if you specify a list of list iterator does not support the set operation.

Automatic Packing and unpacking in Java

This is something new that was introduced after jdk1.5. The Java language specification states that in many cases packing and unpacking are done by the compiler itself (in which case packing is called boxing and unpacking is called unpacking);

In fact, according to my own understanding, automatic boxing can be simply understood as the encapsulation of basic data types into object types, in line with Java object-oriented; For example, int:

// Declare an Integer object

     Integer num = 10;

// The above declaration uses automatic boxing: resolves to

     Integer num = new Integer(10);

This is a good example, because 10 is a primitive data type and cannot be assigned to an object Integer directly in principle, but after jdk1.5 you can declare this, which is the beauty of automatic boxing.

Automatically converts the base data type to the corresponding wrapper type. As an object, you can call all the methods declared by the object.

Automatic unboxing: To convert an object back to its primitive data type:

     / / packing

     Integer num = 10;

     / / split open a case

     int num1 = num;
Copy the code

A typical use of auto-unboxing is in operations: objects cannot be directly evaluated, but must be converted to primitive data types before they can be added, subtracted, multiplied and divided.

     Integer num = 10;

     // Automatic unpacking is implicit in the calculation

     System.out.print(num--);
Copy the code

If you look at an example,

// The number between -128 and 127
Integer num1 = 297;   Integer num2 = 297;           
System.out.println("num1==num2: "+(num1==num2));                    
// The number between -128 and 127
Integer num3 = 97;   Integer num4 = 97;   
System.out.println("num3==num4: "+(num3==num4));
Copy the code

Num1 ==num2: false num3==num4: true

This is due to Java’s design for automatic boxing and unboxing integers and ints in one mode: flyweight.

To increase reuse of simple numbers, Java defines that values between -128 and 127 will be stored in memory for reuse after being boxed as Integer objects during automatic boxing, and only one object will always exist.

If the value between -128 and 127 is exceeded, the boxed Integer object will not be reused, which means that a new Integer object will be created for each boxed object. You get the idea.

The above phenomenon is caused by the use of automatic boxing. If you do not use automatic boxing, but instantiate a new class as normal, you will have a new object every time new.

This kind of auto-boxing applies not only to primitive data types, but also to the String class. For example, we often declare a String:

String str = "sl";
// Replace the following declaration
String str = new String("sl");
Copy the code