2021-07-23 Java基础多线程与Arrays工具类与String工具类的使用

日记

昨天中午下馆子
吃的我撑死了
可惜吃不完浪费了
可惜可惜
今天是在公司自学的第五天
奥里给

多线程实现的方式

新建线程类继承Thread
在类中重写run方法
在主线程中new出线程对象
然后.start()方法启动线程
在线程类的run方法中实现

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("主线程运行中.....");
        }
    }
}
class MyThread extends Thread{
    public void run(){
        for (int i = 0; i < 10000000; i++) {
            System.out.println("线程1运行中");
        }
    }
}

多线程的方法

1 public void start()
使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
2 public void run()
如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
3 public final void setName(String name)
改变线程名称,使之与参数 name 相同。
4 public final void setPriority(int priority)
更改线程的优先级。
5 public final void setDaemon(boolean on)
将该线程标记为守护线程或用户线程。
6 public final void join(long millisec)
等待该线程终止的时间最长为 millis 毫秒。
7 public void interrupt()
中断线程。
8 public final boolean isAlive()
测试线程是否处于活动状态。
测试线程是否处于活动状态。 上述方法是被Thread对象调用的。下面的方法是Thread类的静态方法。

1 public static void yield()
暂停当前正在执行的线程对象,并执行其他线程。
2 public static void sleep(long millisec)
在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
3 public static boolean holdsLock(Object x)
当且仅当当前线程在指定的对象上保持监视器锁时,才返回 true。
4 public static Thread currentThread()
返回对当前正在执行的线程对象的引用。
5 public static void dumpStack()
将当前线程的堆栈跟踪打印至标准错误流。

Arrays类的方法

给数组赋值:通过 fill 方法。

Arrays.fill( a1, value );

a1是一个数组变量,value是一个a1中元素数据类型的值,作用:填充a1数组中的每个元素都是value

package Array类;

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 方法,按升序。

package Array类;

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);
        }
    }
}

在这里插入图片描述

比较数组:通过 equals 方法比较数组中元素值是否相等。

package Array类;

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));
    }
}

在这里插入图片描述

查找数组元素:通过 binarySearch
方法能对排序好的数组进行二分查找法操作。

package Array类;

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;
        }
        //返回索引
        System.out.println(Arrays.binarySearch(s,3));
    }
}

在这里插入图片描述

String类的方法

构造方法
String s = new String("");
String s = “”;
String s = new String(char数组);
String s = new String(char数组,起始下标,长度);
String s = new String(byte数组);
String s = new String(byte数组,起始下标,长度);

常用方法

package String;

import 接口.Student;

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

public class jiujiu {
    public static void main(String[] args) {
        //找出字符串中指定位置的字符
        char c = "中国人".charAt(0);
        System.out.println(c);//中

        //比较两个字符串
        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

        //判断字符串中是否包含指定字符串
        System.out.println("helloworld.java".contains(".java"));//true
        System.out.println("helloworld.java".contains(".jaa"));//false

        //endswith判断是否以指定字符串结尾
        System.out.println("text.txt".endsWith(".txt"));//true

        //判断两个字符串是否相等
        System.out.println("abc".equals("abc"));//true

        //判断两个字符串是否相等并且忽略大小写
        System.out.println("abc".equalsIgnoreCase("ABC"));//true

        //将字符串数组转换成字节数组
        byte b[] = "abcd".getBytes();
        for (byte b1 :
                b) {
            System.out.println(b1);
        }// 97 98 99 100

        //判断某个字符串中指定字符串第一次出现的索引
        //lastindexof则是最后出现的索引
        System.out.println(
                "aaaaaaaabbbbbbbbbbbccccccccccddddddddd".indexOf("ab")
        );//7

        //判断某个字符串是否为空
        System.out.println("".isEmpty());//true

        //替换字符串中的指定字符串为新的字符串
        System.out.println("abcdferdagcvasdfga".replace("df","ppppppp"));//abcppppppperdagcvaspppppppga

        //拆分字符串
        String s[] = "1980-2-2".split("-");
        for (String s1 :
                s) {
            System.out.println(s1);
        }//1980
//         2
//         2

        //判断是否已某个字符串开始
        System.out.println("avcdafodshagkfgvkdbkushanb ".startsWith("a"));
        System.out.println("avcdafodshagkkdbkushanb ".startsWith("ac"));

        //从指定位置开始截取字符串
        System.out.println("www.baidu.com".substring(4));//baidu.com

        //从指定位置开始截取字符串到指定位置为止  左闭右开
        System.out.println("www.baidu.com".substring(4,8));//baid

        //将字符串转换为char数组
        char a[] = "中国人".toCharArray();
        for (char s1 :
                a) {
            System.out.println(s1);
        }//       中
//                国
//                人
        //将字符串中的字母全部转为小写(大写)
        System.out.println("abCDefGH".toLowerCase(Locale.ROOT));//abcdefgh
        System.out.println("abCDefGH".toUpperCase(Locale.ROOT));//ABCDEFGH

        //trim去除字符串前后空格
        System.out.println("  abc   ".trim());//abc

        //将非字符串转换为字符串
        System.out.println(String.valueOf(999));//"999"

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

            @Override
            public String toString() {
                return "$classname{" +
                        "name='" + name + '\'' +
                        '}';
            }
        };
        //调用对象中的toString方法
        System.out.println(String.valueOf(student));//$classname{name='zjn'}

    }
}

所有方法
1 char charAt(int index)
返回指定索引处的 char 值。
2 int compareTo(Object o)
把这个字符串和另一个对象比较。
3 int compareTo(String anotherString)
按字典顺序比较两个字符串。
4 int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。
5 String concat(String str)
将指定字符串连接到此字符串的结尾。
6 boolean contentEquals(StringBuffer sb)
当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
7 static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
8 static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。
9 boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
10 boolean equals(Object anObject)
将此字符串与指定的对象比较。
11 boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。
12 byte[] getBytes()
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
13 byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
15 int hashCode()
返回此字符串的哈希码。
16 int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
17 int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
18 int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
19 int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
20 String intern()
返回字符串对象的规范化表示形式。
21 int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
22 int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
23 int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。
24 int lastIndexOf(String str, int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
25 int length()
返回此字符串的长度。
26 boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
28 boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
29 String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
30 String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
31 String replaceFirst(String regex, String replacement)
使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
32 String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
33 String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
34 boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
35 boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
36 CharSequence subSequence(int beginIndex, int endIndex)
返回一个新的字符序列,它是此序列的一个子序列。
37 String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
38 String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
39 char[] toCharArray()
将此字符串转换为一个新的字符数组。
40 String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
41 String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
42 String toString()
返回此对象本身(它已经是一个字符串!)。
43 String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
44 String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
45 String trim()
返回字符串的副本,忽略前导空白和尾部空白。
46 static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。
47 contains(CharSequence chars)
判断是否包含指定的字符系列。
48 isEmpty()
判断字符串是否为空。

StringBuffer与StringBuilder

频繁拼接字符串用StringBuffer或者StringBuilder
StringBuilder是线程不安全的
StringBuffer是线程安全的

package String;

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

        //字符串拼接
        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三者转换

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对象)
在这里插入图片描述

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值