2021-07-23 use of java basic multithreading and Arrays tool class and String tool class

diary

I went out at noon yesterday
I'm dead
It's a pity that we can't finish it and waste it
It's a pity
Today is the fifth day of self-study in the company
Origi

Multithreading implementation

New Thread class inherits Thread
Override the run method in the class
new thread object in main thread
then. The start() method starts the thread
It is implemented in the run method of thread class

public class ThreadTest01 {
    public static void main(String[] args) {
        Thread thread = new MyThread();
        thread.start();
        for (int i = 0; i < 10000000; i++) {
            System.out.println("The main thread is running.....");
        }
    }
}
class MyThread extends Thread{
    public void run(){
        for (int i = 0; i < 10000000; i++) {
            System.out.println("Thread 1 is running");
        }
    }
}

Multithreaded method

1 public void start()
Start the thread to execute; The Java virtual machine calls the run method of the thread.
2 public void run()
If the thread is constructed using an independent Runnable running object, call the run method of the Runnable object; Otherwise, the method does nothing and returns.
3 public final void setName(String name)
Change the thread name to be the same as the parameter name.
4 public final void setPriority(int priority)
Change the priority of the thread.
5 public final void setDaemon(boolean on)
Mark the thread as a daemon or user thread.
6 public final void join(long millisec)
The maximum time to wait for the thread to terminate is millis econds.
7 public void interrupt()
Interrupt the thread.
8 public final boolean isAlive()
Test whether the thread is active.
Test whether the Thread is active. The above method is called by the Thread object. The following method is a static method of the Thread class.

1 public static void yield()
Pauses the currently executing thread object and executes other threads.
2 public static void sleep(long millisec)
Hibernate (pause execution) the currently executing thread for a specified number of milliseconds. This operation is affected by the accuracy and accuracy of the system timer and scheduler.
3 public static boolean holdsLock(Object x)
Returns true if and only if the current thread holds a monitor lock on the specified object.
4 public static Thread currentThread()
Returns a reference to the thread object currently executing.
5 public static void dumpStack()
Prints the stack trace of the current thread to the standard error stream.

Methods of the Arrays class

Assign a value to the array: fill method.

Arrays.fill( a1, value );

a1 is an array variable, and value is a value of the element data type in a1. Function: each element in the a1 array is filled with value

package Array class;

import java.util.Arrays;

public class fill {
    public static void main(String[] args) {
        int ss[] = new int[5];
        int x=2;
        Arrays.fill(ss,2);
        for (int i :
                ss) {
            System.out.println(i);
        }
    }
}

Sort the array: sort in ascending order through the sort method.

package Array class;

import java.util.Arrays;

public class fill {
    public static void main(String[] args) {
        int s[] = new int[5];
        for (int i = 0; i < s.length; i++) {
            s[i] = 5-i;
        }
        Arrays.sort(s);
        for (int i :
                s) {
            System.out.println(i);
        }
    }
}

Compare arrays: use the equals method to compare whether the element values in the array are equal.

package Array class;

import java.util.Arrays;

public class fill {
    public static void main(String[] args) {
        int s[] = new int[5];
        for (int i = 0; i < s.length; i++) {
            s[i] = 5-i;
        }
        Arrays.sort(s);
        int ss[] = new int[5];
        for (int i = 0; i < ss.length; i++) {
            ss[i] = 5-i;
        }
        Arrays.sort(ss);
        System.out.println(Arrays.equals(s,ss));
    }
}

Find array elements: through binarySearch
Method can perform binary search operation on the sorted array.

package Array class;

import java.util.Arrays;

public class fill {
    public static void main(String[] args) {
        int s[] = new int[5];
        for (int i = 0; i < s.length; i++) {
            s[i] = 5-i;
        }
        //Return index
        System.out.println(Arrays.binarySearch(s,3));
    }
}

Method of String class

Construction method
String s = new String("");
String s = "";
String s = new String(char array);
String s = new String(char array, starting subscript, length);
String s = new String(byte array);
String s = new String(byte array, starting subscript, length);

common method

package String;

import Interface.Student;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

public class jiujiu {
    public static void main(String[] args) {
        //Finds the character at the specified position in the string
        char c = "Chinese".charAt(0);
        System.out.println(c);//in

        //Compare two strings
        int result = "abc".compareTo("abc");
        System.out.println(result);//0
        int result2 = "abcd".compareTo("abce");
        System.out.println(result2);//-1
        int result3 = "abcd".compareTo("abcc");
        System.out.println(result3);//1

        //Determines whether the specified string is included in the string
        System.out.println("helloworld.java".contains(".java"));//true
        System.out.println("helloworld.java".contains(".jaa"));//false

        //Endswitch determines whether to end with the specified string
        System.out.println("text.txt".endsWith(".txt"));//true

        //Determine whether two strings are equal
        System.out.println("abc".equals("abc"));//true

        //Determines whether two strings are equal and ignores case
        System.out.println("abc".equalsIgnoreCase("ABC"));//true

        //Converts a string array to a byte array
        byte b[] = "abcd".getBytes();
        for (byte b1 :
                b) {
            System.out.println(b1);
        }// 97 98 99 100

        //Determines the index of the first occurrence of a specified string in a string
        //Last index of is the last index
        System.out.println(
                "aaaaaaaabbbbbbbbbbbccccccccccddddddddd".indexOf("ab")
        );//7

        //Determine whether a string is empty
        System.out.println("".isEmpty());//true

        //Replace the specified string in the string with a new string
        System.out.println("abcdferdagcvasdfga".replace("df","ppppppp"));//abcppppppperdagcvaspppppppga

        //Split string
        String s[] = "1980-2-2".split("-");
        for (String s1 :
                s) {
            System.out.println(s1);
        }//1980
//         2
//         2

        //Determines whether a string has started
        System.out.println("avcdafodshagkfgvkdbkushanb ".startsWith("a"));
        System.out.println("avcdafodshagkkdbkushanb ".startsWith("ac"));

        //Intercepts the string from the specified location
        System.out.println("www.baidu.com".substring(4));//baidu.com

        //Intercept the string from the specified position until the specified position is closed on the left and open on the right
        System.out.println("www.baidu.com".substring(4,8));//baid

        //Convert string to char array
        char a[] = "Chinese".toCharArray();
        for (char s1 :
                a) {
            System.out.println(s1);
        }//       in
//                country
//                people
        //Convert all letters in the string to lowercase (uppercase)
        System.out.println("abCDefGH".toLowerCase(Locale.ROOT));//abcdefgh
        System.out.println("abCDefGH".toUpperCase(Locale.ROOT));//ABCDEFGH

        //trim removes spaces before and after a string
        System.out.println("  abc   ".trim());//abc

        //Convert non string to string
        System.out.println(String.valueOf(999));//"999"

        Student student = new Student(){
            String name = "zjn";

            @Override
            public String toString() {
                return "$classname{" +
                        "name='" + name + '\'' +
                        '}';
            }
        };
        //Call the toString method in the object
        System.out.println(String.valueOf(student));//$classname{name='zjn'}

    }
}

All methods
1 char charAt(int index)
Returns the char value at the specified index.
2 int compareTo(Object o)
Compare this string with another object.
3 int compareTo(String anotherString)
Compares two strings in dictionary order.
4 int compareToIgnoreCase(String str)
Compares two strings in dictionary order, regardless of case.
5 String concat(String str)
Concatenates the specified string to the end of this string.
6 boolean contentEquals(StringBuffer sb)
Returns true if and only if the string has characters in the same order as the specified StringBuffer.
7 static String copyValueOf(char[] data)
Returns a String representing the character sequence in the specified array.
8 static String copyValueOf(char[] data, int offset, int count)
Returns a String representing the character sequence in the specified array.
9 boolean endsWith(String suffix)
Tests whether this string ends with the specified suffix.
10 boolean equals(Object anObject)
Compares this string with the specified object.
11 boolean equalsIgnoreCase(String anotherString)
Compare this String with another String, regardless of case.
12 byte[] getBytes()
Encode this String as a byte sequence using the platform's default character set, and store the result in a new byte array.
13 byte[] getBytes(String charsetName)
Encodes this String into a byte sequence using the specified character set, and stores the result in a new byte array.
14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copy characters from this string to the target character array.
15 int hashCode()
Returns the hash code of this string.
16 int indexOf(int ch)
Returns the index of the first occurrence of the specified character in this string.
17 int indexOf(int ch, int fromIndex)
Returns the index at the first occurrence of the specified character in this string, starting with the specified index.
18 int indexOf(String str)
Returns the index of the specified substring at the first occurrence in this string.
19 int indexOf(String str, int fromIndex)
Returns the index of the specified substring at the first occurrence in this string, starting from the specified index.
20 String intern()
Returns a normalized representation of a string object.
21 int lastIndexOf(int ch)
Returns the index of the last occurrence of the specified character in this string.
22 int lastIndexOf(int ch, int fromIndex)
Returns the index of the last occurrence of the specified character in this string, starting with the specified index.
23 int lastIndexOf(String str)
Returns the index of the rightmost occurrence of the specified substring in this string.
24 int lastIndexOf(String str, int fromIndex)
Returns the index of the last occurrence of the specified substring in this string, and searches backwards from the specified index.
25 int length()
Returns the length of this string.
26 boolean matches(String regex)
Tells whether this string matches the given regular expression.
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
28 boolean regionMatches(int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
29 String replace(char oldChar, char newChar)
Returns a new string obtained by replacing all oldchars that appear in this string with newChar.
30 String replaceAll(String regex, String replacement)
Replace all substrings of this string that match the given regular expression with the given replacement.
31 String replaceFirst(String regex, String replacement)
Replace this string with the given replacement to match the first substring of the given regular expression.
32 String[] split(String regex)
Splits the string based on the match of the given regular expression.
33 String[] split(String regex, int limit)
Splits the string based on matching the given regular expression.
34 boolean startsWith(String prefix)
Tests whether this string starts with the specified prefix.
35 boolean startsWith(String prefix, int toffset)
Tests whether the substring of this string starting from the specified index starts with the specified prefix.
36 CharSequence subSequence(int beginIndex, int endIndex)
Returns a new character sequence, which is a subsequence of this sequence.
37 String substring(int beginIndex)
Returns a new string, which is a substring of this string.
38 String substring(int beginIndex, int endIndex)
Returns a new string, which is a substring of this string.
39 char[] toCharArray()
Converts this string to a new character array.
40 String toLowerCase()
Convert all characters in this String to lowercase using the rules of the default locale.
41 String toLowerCase(Locale locale)
Converts all characters in this String to lowercase using the rules of the given Locale.
42 String toString()
Returns the object itself (it is already a string!).
43 String toUpperCase()
Convert all characters in this String to uppercase using the rules of the default locale.
44 String toUpperCase(Locale locale)
Convert all characters in this String to uppercase using the rules of the given Locale.
45 String trim()
Returns a copy of a string, ignoring leading and trailing whitespace.
46 static String valueOf(primitive data type x)
Returns the string representation of the x parameter of the given data type.
47 contains(CharSequence chars)
Determines whether the specified character series is included.
48 isEmpty()
Determine whether the string is empty.

StringBuffer and StringBuilder

String buffer or StringBuilder is used to frequently splice strings
StringBuilder is thread unsafe
StringBuffer is thread safe

package String;

public class StringBufferTest {
    public static void main(String[] args) {

        //String splicing
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("a");
        stringBuffer.append("a");
        stringBuffer.append("a");
        stringBuffer.append("a");


        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("a");
        stringBuilder.append("a");
        stringBuilder.append("a");
        stringBuilder.append("a");
        

    }
}

String int integer conversion

int->String String.valueOf(int)
String->int Integer.parseInt("123")
int->Integer Integer.valueOf("123")
Integer->int intValue()
String->Integer Integer.valueOf("123")
Integer——>String String. Valueof (integer object)

public class StringIntIntegerzhuanhuan {
    public static void main(String[] args) {
        int x = 10;
        Integer X = x;
        System.out.println(X);

        int q = X;
        System.out.println(q);

        String s = String.valueOf(x);
        System.out.println(s);

        int S = Integer.parseInt(s);
        System.out.println(S);

        String p = String.valueOf(X);
        System.out.println(p);

        Integer integer = Integer.valueOf(p);
        System.out.println(integer);
    }
}

Keywords: Java

Added by dandelo on Sat, 15 Jan 2022 08:55:29 +0200