Java API shorthand manual (continuously updated ing...)

The gods are silent-personal CSDN blog post directory

The reason why I did this is very simple, because I haven't written Java in 3 years and I am currently recovering.
Because I have been using Python recently, I will not write the same parts as Python.

  1. The most basic framework
    public class MainClass {
          
          
        public static void main(String[] args) {
          
          
            //主函数代码
        }
    }
    
  2. Print:System.out.println(打印内容);
  3. Java array of built-in objects
    (to be continued)
  4. instantiate object
    1. built-in objects
      int x=1;
    2. Custom objects:类名 实例名=new 类名();
  5. Object type conversion
    1. Convert float to int 1
      1. Float classintValue()
      Float f = 10.5f;
      int i = f.intValue();
      System.out.println("Float转Int:" + i);
      
      1. cast
      float f1 = 10.5f;
      int i1 = (int) f1;
      System.out.println("浮点型强制转换为整型:" + i1);
      
  6. Loop ( Java loop (to be continued) )
  7. Conditional statement 2
    1. if
      if(布尔表达式)
      {
              
              
         //如果布尔表达式为true将执行的语句
      }
      
    2. if-else
      if(布尔表达式){
              
              
         //如果布尔表达式的值为true
      }else{
              
              
         //如果布尔表达式的值为false
      }
      
  8. static static method
    private private method
    final
  9. Arrays class
    1. Assignmentfill()
      public static void fill(arrayname,value)
      public static void fill(arrayname ,starting index ,ending index ,value)
    import java.util.*;
    public class Example{
          
          
        public static void main(String[] args) {
          
          
            int array[] = new int[10];
            Arrays.fill(array, 1);
            for (int arrays:array) {
          
          
                System.out.print(arrays+" ");
            }
            System.out.println();
            Arrays.fill(array, 3, 6, 9);
            for (int arrays:array) {
          
          
                System.out.print(arrays+" ");
            }
        }
    }
    
    Output:
    1 1 1 1 1 1 1 1 1 1 
    1 1 1 9 9 9 1 1 1 1 
    
    1. Sort sort()
      public static void sort(Object[] arrayname)all the elements of an array, and sort the array part in order from small to large,
      public static void sort(Object[] arrayname,int fromIndex, int toIndex)that is, sort the elements of array a from fromIndex to toIndex-1.
      import java.util.*;
      public class Example{
              
              
          public static void main(String[] args) {
              
              
              int array[] = {
              
              2,5,85,30,75,66,-18,0};
              Arrays.sort(array,2,5);
              for (int arrays:array) {
              
              
                  System.out.print(arrays+" ");
              }
              System.out.println();
              Arrays.sort(array);
              for (int arrays:array) {
              
              
                  System.out.print(arrays+" ");
              }
          }
      }
      
      Output:
      2 5 30 75 85 66 -18 0 
      -18 0 2 5 30 66 75 85 
      
      The underlying principle of Arrays.sort():
      Assume that the array length is n
      1<=n<47, use insertion sort
      47<=n<286, use quick sort
      n>=286, use merge sort or quick sort (there is a certain order to use Merge sort, use quick sort in no order)
    2. Search binarySearch()
      Use binary search: the array must be sorted before calling.
      public static int binarySearch(Object[] a,Object key)Search in all elements of an array.

      Return value:
      Within the range of the array, the index value is "-the insertion point index value"
      is less than the element in the array, the index value is - 1
      Greater than the element in the array, the index value is – (length + 1).

      public static int binarySearch(Object[] a,int fromIndex,int toIndex,Object key)Search within the range specified by the array.

      Return value:
      Within the search range, the index value is “-Insertion point index value”. If it
      is less than the element in the search range, return –( fromIndex + 1)
      is greater than the element in the search range, return – (toIndex + 1)
      import java.util.*;
      public class Example{
              
              
          public static void main(String[] args) {
              
              
              int array[] = {
              
              2,5,85,30,75,66,-18,0};
              Arrays.sort(array);
              for (int arrays:array) {
              
              
                  System.out.print(arrays+" ");
              }
              System.out.println();
              System.out.println(Arrays.binarySearch(array,5));
              System.out.println(Arrays.binarySearch(array,-99));
              System.out.println(Arrays.binarySearch(array,100));
              System.out.println(Arrays.binarySearch(array,60));
              System.out.println(Arrays.binarySearch(array,1,5,5));
              System.out.println(Arrays.binarySearch(array,1,5,-99));
              System.out.println(Arrays.binarySearch(array,1,5,100));
              System.out.println(Arrays.binarySearch(array,1,5,60));
          }
      }
      
      Output:
      -18 0 2 5 30 66 75 85 
      3         //5在数组内,返回排完序后的索引3
      -1        //-99小于数组内元素,返回索引值为-1
      -9        //100大于数组内元素,返回索引值为-(length+1)=-(8+1)
      -6        //60在数组范围内,返回索引值为-插入点索引值=-6
      3         //5在搜索范围内,返回排完序后的索引3
      -2        //-99小于搜索范围内元素,返回–(fromIndex + 1)=-(1+1)=-2
      -6        //100大于搜索范围内元素,返回–(toIndex + 1)=-(5+1)=-6
      -6        //60在搜索范围内,索引值为-插入点索引值=-6`
      
    3. Comparison equals()
      Returns true if the two specified arrays are equal to each other. Two arrays are considered equal if they contain the same number of elements and all corresponding pairs of elements in both arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order.
      public static boolean equals(Object[] arrayname,Object[] arrayname2)
    import java.util.*;
    public class Example{
          
          
        public static void main(String[] args) {
          
          
            int[] array1 = {
          
          2,5,85,30,75,66,-18,0};
            int[] array2 = {
          
          75,2,66,30,5,85,0,-18};
            
            if(Arrays.equals(array1, array2)){
          
          
                System.out.println("array1等于array2");
            }
            else{
          
          
                System.out.println("array1不等于array2");
            }
            
            Arrays.sort(array1);
            Arrays.sort(array2);
            
            for(int arrays:array1){
          
          
                System.out.print(arrays+" ");
            }
            System.out.println();
            for(int arrays:array2){
          
          
                System.out.print(arrays+" ");
            }     
            System.out.println();
            
            if(Arrays.equals(array1, array2)){
          
          
                System.out.println("排序后,array1等于array2");
            }
            else{
          
          
                System.out.println("排序后,array1不等于array2");
            }
        }
    }
    
    Output:
    array1不等于array2
    -18 0 2 5 30 66 75 85 
    -18 0 2 5 30 66 75 85 
    排序后,array1等于array2
    
    1. Copy Copy
      copyOf()the elements of the original array to a new array. You can set the length of the copy (that is, the number of elements that need to be copied). public static Object[] copyOf(original,newLength)

      copyOfRange()Copy elements within a certain range to the new array. public static Object[] copyOfRange(original,int from,int to)from is the starting position of the copy (including ), to is the end position of the copy (not included)
    import java.util.*;
    public class Example{
          
          
        public static void main(String[] args) {
          
          
            int[] array1 = {
          
          2,5,85,30,75,66,-18,0};
            int[] array2 = Arrays.copyOf(array1, 6);
            int[] array3 = Arrays.copyOfRange(array1, 2, 4);
            System.out.println(Arrays.toString(array1));
            System.out.println(Arrays.toString(array2));
            System.out.println(Arrays.toString(array3));       
        }
    }
    
    Output:
    [2, 5, 85, 30, 75, 66, -18, 0]
    [2, 5, 85, 30, 75, 66]
    [85, 30]
    
  10. Deque<Integer> stack = new ArrayDeque<Integer>();
    stack.push(Integer.parseInt(arr[i]));
    stack.peek()
    stack.pop();
  11. List List<Integer> list = new ArrayList<Integer>();
    list to string:String str = list.toString(); String.join(" ", nums);
    list.add(factor);
  12. Split the string into an array by delimiter:String[] arr = data.split(", "); nums = Arrays.asList(data.split(" "));
  13. String slicing:str.substring(1, str.length() - 1);
  14. Check whether the object is None (None in Java means that it is not created by just declaring it):data.isEmpty()
  15. Properties and methods:length size()
  16. Integer.MIN_VALUE
    Integer.MAX_VALUE
  17. Number to string:String.valueOf(root.val)
  18. Integer.parseInt(nums.get(i))
  19. Math class
    1. min()3
      public class Test{
              
              
          public static void main(String args[]){
              
              
              System.out.println(Math.min(12.123, 12.456));      
              System.out.println(Math.min(23.12, 23.0));  
          }
      }
      
      Output result:
      12.123
      23.0
      

Other references used in writing this article:

  1. Java briefly talks about the difference between array (Array) and list (ArrayList) and introduces common methods of Arrays_The difference between array and list_senxu_'s blog-CSDN blog

  1. How to convert Float to Int type in Java? - IT Vision ↩︎

  2. Java conditional statement – ​​if…else | Newbie tutorial↩︎

  3. Java min() method | rookie tutorial ↩︎

Guess you like

Origin blog.csdn.net/PolarisRisingWar/article/details/132195488