当前位置:网站首页>Decrypt the responsive layout of the flutter

Decrypt the responsive layout of the flutter

2020-12-06 23:33:34 SSS soars thousands of miles

29 Common tools ( One )

1.Arrays------ Handling arrays

1.1 copyOf Method

Underlying implementation : call System.arrayCopy

public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
// Additional explanation 
// The argument is int An array type copeOf A piece of source code 
// Actually in Arrays Class in the source code , Yes 8 Corresponding to the array of basic data types copeOf Method , Its core is to call System.arraycopy Method 
//Arrays Class copyOf The return value of is the basic data type array corresponding to the actual parameter , If the source code above returns int[] Array of 
//Arrays Class copeOf And System.arraycopy The difference is as follows 
   1.Arrays Class copeOf The essence of a method is to call System.arraycopy Method 
   2.Arrays Class copeOf Can achieve 8 Replication of arrays of basic data types , And it returns the address of a new array ;
   3.System.arrayCopy Method has no return value , The bottom layer is made of native Methods of keyword modification ( The original ecological method ), Call non java The method of language 
   4.System Ask to create a new array when copying , And pass it in as an argument 
// Operation example 
 int[] nums={1,66,4,3,-2};                                                           
 int[] nums01=Arrays.copyOf(nums,nums.length+1);// The second parameter is the copied length                
 System.out.println(" The new array is shown below ");                           
 for (int i = 0; i < nums01.length; i++) {                                           
    System.out.print(nums01[i]+"\t");                                               
 }                                                                                   

1.2 toString Method ( Print the contents of array elements as strings )

The essence : It's rewritten Object Of toString Method

public static String toString(int[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }
    //StringBuffer It's safe, but it's not efficient ,StringBuilder Not safe but efficient , So the synthesis of many 
// Operation example 
int[] nums={1,66,4,3,-2};                                     
System.out.println(" use toString Method to print the array :"+Arrays.toString(nums));   

1.3 sort Method ( Default ascending order , Quick line up )

 int nums[]={1,8,99,40,77};                            
 Arrays.sort(nums);// The original array will complete the ascending operation through the quick sort principle                 
 for (int num : nums) {                                
     System.out.print(num+"\t");                       
 }
 //sort The return value of the method is void

1.4 fill Method ( Assign all values in the array to the same )

// Source code is as follows :( There are multiple overloads fill Method , Chose one )
public static void fill(long[] a, long val) {
        for (int i = 0, len = a.length; i < len; i++)
            a[i] = val;
}
int[] nums={1,66,4,3,-2};                 
Arrays.fill(nums,100);                     
 for (int i = 0; i < nums.length; i++) {   
    System.out.print(nums[i]+"\t");        
 }
 //fill The return value of the method is void

2.System class ( System tool class )

2.1 Exit procedure (exit)

effect : Terminate the currently executing java virtual machine ( Not 0 To terminate abnormally )

The essence is to call Runtime Class exit Method

//System.exit Source code is as follows :
public static void exit(int status) {
      Runtime.getRuntime().exit(status);
}
//Runtime.getRuntime()  The return value is the same as the current java Application related Runtime object 
//Runtime Object instances are in each java There will always be a , Its role is to enable applications to connect to the environment in which they run 
// Be careful : Applications cannot create their own Runtime Class instance 
//Runtime The source code in the class is as follows :
  public static Runtime getRuntime() {
        return currentRuntime;
 }
 // This currentRuntime It has been instantiated (47 That's ok )
 exit Method source code 
public void exit(int status) {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkExit(status);
        }
        Shutdown.exit(status);
}

2.2 Get the current time ( distance 1900 year 1 month 1 The millisecond value of ,1s=1000 millisecond )

 System.out.println(System.currentTimeMillis());
 // By getting the time twice , And then subtract , You can get the running time of a part of the program 

2.3 The assignment of an array ( Manual expansion )

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

src Represents the address of the source array ,dest Represents the address of the target array ;

srcPos The source array gets the replacement element ( Let another element be replaced ) Start of ,destPos The starting position of the target array replacement ( Replaced by a replacement element )

length Is the total length of the replacement

   int[] a={1,8,99,66};
   int[] b=new int[4];
   // Replace the element with 1,8,99
   // The starting position of the replacement is b[1]
   System.arraycopy(a,0,b,1,a.length-1);
   for (int i = 0; i < b.length; i++) {
       System.out.print(b[i]+"\t");
   }
   // The first one is unassigned , The system will automatically assign a default value 

3.Date class ( And SimpleDateFormat Class )

​ Create objects by default , For the current time

The usage method is shown below :

 Step one    Instantiate a Date object 
 Step two    Instantiate a SimpleDateFormat object , Input the required format 
 Step three     call format Method ( Print out the current time )---- The return is String Type of date information 

( use String Variable to hold it )

 Date date = new Date();
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM//dd HH:mm:ss");
 String str=simpleDateFormat.format(date);
System.out.println(str);

4.Calendar

//locale For the language environment ,TImezone Is the time zone

The operation flow of outputting calendar related information is as follows :

  First step . Declare a calendar object , Point to a calendar , The calendar is through getInstance Static method to get the calendar of default time zone and locale ,
 The second step , According to the demand , adopt get Method to get the information you want ( year 、 month 、 Japan 、 Zhou )----> Given the value of the calendar field 
  Be careful : These calendar fields are global constants (static final)
Calendar calendar=Calendar.getInstance();         System.out.println(calendar.get(Calendar.DAY_OF_MONTH));// What day of the month        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));// What day of the week        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));// The day of the year 

版权声明
本文为[SSS soars thousands of miles]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206233102886w.html