zoukankan      html  css  js  c++  java
  • 一些工具类

    代码1

    /*
    java 语言中线程共有六种状态,分别是:
    
    NEW(初始化状态)
    RUNNABLE(可运行 / 运行状态)
    BLOCKED(阻塞状态)
    WAITING(无时限等待)
    TIMED_WAITING(有时限等待)
    TERMINATED(终止状态)
    */
    public class Demo1 {
        public static void main(String[] args) {
            System.out.println("Math.abs(-10) = " + Math.abs(-10));
            System.out.println("Math.ceil(3.14) = " + Math.ceil(3.14));
            System.out.println("Math.floor(9.8) = " + Math.floor(9.8));
            System.out.println("Math.random() = " + Math.random());
            System.out.println("Math.pow(2,4) = " + Math.pow(2, 4));
            System.out.println("Math.sqrt(9) = " + Math.sqrt(9));
            System.out.println("Math.round(3.14) = " + Math.round(3.14));
            System.out.println("Math.round(3.4) = " + Math.round(3.4));
            System.out.println("Math.round(3.5) = " + Math.round(3.5));
            System.out.println("Math.PI = " + Math.PI);
        }
    }

    输出结果

    Math.abs(-10) = 10
    Math.ceil(3.14) = 4.0
    Math.floor(9.8) = 9.0
    Math.random() = 0.8814462121707075
    Math.pow(2,4) = 16.0
    Math.sqrt(9) = 3.0
    Math.round(3.14) = 3
    Math.round(3.4) = 3
    Math.round(3.5) = 4
    Math.PI = 3.141592653589793
    
    Process finished with exit code 0

    代码2

    import java.math.BigDecimal;
    import java.math.BigInteger;
    
    /*
    java 语言中线程共有六种状态,分别是:
    
    NEW(初始化状态)
    RUNNABLE(可运行 / 运行状态)
    BLOCKED(阻塞状态)
    WAITING(无时限等待)
    TIMED_WAITING(有时限等待)
    TERMINATED(终止状态)
    */
    public class Demo1 {
        public static void main(String[] args) {
    //        BigInteger 存储大的整数
            System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
            BigInteger bigInteger1 = new BigInteger("9223372036854775807");
            System.out.println("bigInteger1 = " + bigInteger1);
            BigInteger b1 = new BigInteger("20");
            BigInteger b2 = new BigInteger("2");
            System.out.println("b1.add(b2) = " + b1.add(b2));
            System.out.println("b1.divide(b2) = " + b1.divide(b2));
            System.out.println("b1.multiply(b2) = " + b1.multiply(b2));
            System.out.println("b1.subtract(b2) = " + b1.subtract(b2));
            System.out.println("b1.remainder(b2) = " + b1.remainder(b2));
            System.out.println("---------------------------------");
    //    BigDecimal
            BigDecimal bd1 = new BigDecimal(20);
            BigDecimal bd2 = new BigDecimal(10);
            System.out.println("bd1.add(bd2) = " + bd1.add(bd2));
            System.out.println("bd1.divide(bd2) = " + bd1.divide(bd2));
            System.out.println("bd1.multiply(bd2) = " + bd1.multiply(bd2));
            System.out.println("bd1.multiply(bd2) = " + bd1.subtract(bd2));
            System.out.println("bd1.remainder(bd2) = " + bd1.remainder(bd2));
            System.out.println("---------------------------------");
    
            BigDecimal db3 = new BigDecimal(3.0);
    //这样会报错  System.out.println("bd1.divide(db3) = " + bd1.divide(db3));,所以要像下面才行
            System.out.println("bd1.divide(db3) = " + bd1.divide(db3,BigDecimal.ROUND_FLOOR));
    
            //也可以选择小数点的范围
            BigDecimal db4 = new BigDecimal(10.0);
            System.out.println("db4.divide(db3) = " + db4.divide(db3,10,BigDecimal.ROUND_FLOOR));
        }
    }

    输出

    Long.MAX_VALUE = 9223372036854775807
    bigInteger1 = 9223372036854775807
    b1.add(b2) = 22
    b1.divide(b2) = 10
    b1.multiply(b2) = 40
    b1.subtract(b2) = 18
    b1.remainder(b2) = 0
    ---------------------------------
    bd1.add(bd2) = 30
    bd1.divide(bd2) = 2
    bd1.multiply(bd2) = 200
    bd1.multiply(bd2) = 10
    bd1.remainder(bd2) = 0
    ---------------------------------
    bd1.divide(db3) = 6
    db4.divide(db3) = 3.3333333333

    代码3

    import java.util.Random;
    
    public class Demo2 {
        public static void main(String[] args) {
            Random random = new Random();
            for (int i = 0; i <10 ; i++) {
                System.out.println(random.nextInt(10));
            }
    
            System.out.println("--------------");
            System.out.println("random.nextDouble() = " + random.nextDouble());
            System.out.println("random.nextBoolean() = " + random.nextBoolean());
            /*
            random.nextDouble() = 0.6696714377906926
            random.nextBoolean() = false
            * */
    
            System.out.println("--------------");
            Random random2 = new Random(1);
            for (int i = 0; i < 5; i++) {
                System.out.println(random2.nextInt(10));
            }
        }
    }

    代码4

            Date date = new Date();
            System.out.println("date = " + date);
            //日期
            System.out.println("date.getDate() = " + date.getDate());
            //0是第一个月
            System.out.println("date.getMonth() = " + date.getMonth());
            //一周的第几天
            System.out.println("date.getDay() = " + date.getDay());
            //加上1900是现在的年份
            System.out.println(date.getYear()+1900);
    
            long time=date.getTime();
            System.out.println("time = " + time);//time = 1640874836661
    
            Date date2 = new Date(time);
            System.out.println(date2); //Thu Dec 30 22:35:25 CST 2021
    
            String s1 = date2.toLocaleString();
            System.out.println("s1 = " + s1);//s1 = 2021-12-30 22:37:01

    代码5

    import java.text.*;
    import java.time.*;
    import java.util.Calendar;
    import java.util.Date;
    
    /*
    Calendar抽象类不能直接创建对象
    需要通过子类实现功能或者调用getInstance()创建Calendar对象
    * */
    public class Demo3 {
        public static void main(String[] args) throws ParseException {
            Calendar calendar = Calendar.getInstance();
            System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR));//calendar.get(Calendar.YEAR) = 2021
            //月份从0开始
            System.out.println("calendar.get(Calendar.MONTH) = " + calendar.get(Calendar.MONTH));//calendar.get(Calendar.MONTH) = 11
            System.out.println("calendar.get(Calendar.HOUR) = " + calendar.get(Calendar.HOUR));//calendar.get(Calendar.HOUR) = 10
    
            calendar.add(Calendar.YEAR, 2);
            System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR));//calendar.get(Calendar.YEAR) = 2023
    
            calendar.set(1988, 12, 12);
            System.out.println("calendar.getTime() = " + calendar.getTime());//calendar.getTime() = Thu Jan 12 22:19:23 CST 1989
    
            System.out.println(" -------------------- ");
    
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss E");
            Date date= new Date();
            System.out.println("date = " + date);//date = Fri Dec 31 22:26:04 CST 2021
            String result=df.format(date);
            String result2=df2.format(date);
            System.out.println("result = " + result);//result = 2021-12-31 10:26:04
            System.out.println("result2 = " + result2);//result2 = 2021-12-31 10:26:36 星期五
    
            String dateStr="2021-12-31 10:26:39";
            Date date3=df.parse(dateStr);
            System.out.println("date3 = " + date3);//date3 = Fri Dec 31 10:26:39 CST 2021
    
            System.out.println(" -------------------- ");
    
            LocalDate now = LocalDate.now();
            System.out.println("now = " + now);//now = 2021-12-31
            int year= now.getYear();
            System.out.println("year = " + year);//year = 2021
            Month month = now.getMonth();
            System.out.println("month.getValue() = " + month.getValue());//month.getValue() = 12
    
            System.out.println(" -------------------- ");
            LocalDate localDate = now.minusDays(2);
            System.out.println("now = " + now);//now = 2021-12-31
            System.out.println("localDate = " + localDate);//localDate = 2021-12-29
    
            System.out.println(" -------------------- ");
            LocalDate localDate1=LocalDate.of(2020,1,1);
            System.out.println("localDate1 = " + localDate1);//localDate1 = 2020-01-01
            LocalDate localDate2=localDate1.plusDays(2);
            System.out.println("localDate2 = " + localDate2);//localDate2 = 2020-01-03
            Period period= Period.between(localDate,localDate2);
            System.out.println("period = " + period);//period = P-1Y-11M-26D
            System.out.println("period.getYears() = " + period.getYears());//getYears() = -1
            System.out.println("period.getMonths() = " + period.getMonths());//getMonths() = -11
            System.out.println("period.getDays() = " + period.getDays());//-26
    
            System.out.println(" -------------------- ");
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println("localDateTime = " + localDateTime);//localDateTime = 2021-12-31T22:49:06.134
            LocalDateTime localDateTime1=LocalDateTime.of(2021,1,1,10,10,20);
            System.out.println("localDateTime1 = " + localDateTime1);//localDateTime1 = 2021-01-01T10:10:20
    
            Duration duration = Duration.between(localDateTime,localDateTime1);
            System.out.println("duration = " + duration);//PT-8748H-48M-40.025S
            System.out.println("duration.toDays() = " + duration.toDays());// -364
            System.out.println("duration.toHours() = " + duration.toHours());//-8748
    
            System.out.println(" -------------------- ");
            LocalTime localTime = LocalTime.now();
            System.out.println("localTime = " + localTime);
            LocalTime localTime1 = LocalTime.of(12,12,12);
            System.out.println("localTime1 = " + localTime1);
    
    
    
    
    
    
        }
    }

    代码6

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class Demo4 {
        public static void main(String[] args) {
            DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
            LocalDateTime localDateTime1 = LocalDateTime.now();
            String format = formatter.format(localDateTime1);
            System.out.println("format = " + format);//format = 2022-01-01
    
            System.out.println("-----------DateTimeFormatter-----------");
    
            DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss E");
            String format1 = formatter1.format(localDateTime1);
            System.out.println("format1 = " + format1);//format1 = 2022-01-01 11:37:37 星期六
    
            DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format2 = formatter2.format(localDateTime1);
            System.out.println("format2 = " + format2);//format2 = 2022-01-01 11:40:34
    
            String date1 = "2022-01-01 11:40:00";
            LocalDateTime localDateTime2 = LocalDateTime.parse(date1,formatter2);
            System.out.println("localDateTime2 = " + localDateTime2);//localDateTime2 = 2022-01-01T11:40
    
            System.out.println("-----------System-----------");
            long long1 = System.currentTimeMillis();
            System.out.println("long1 = " + long1);//long1 = 1641009919887
    
            int[] arr ={10,22,33};
            int[] newAr =new int[arr.length];
            System.arraycopy(arr,0,newAr,0,3);
            for (int i = 0; i <newAr.length ; i++) {
                System.out.println(newAr[i]);
            }
            System.out.println("-----------Runtime-----------");
            Runtime runtime1 = Runtime.getRuntime();
            Runtime runtime2 = Runtime.getRuntime();
            System.out.println(runtime1 == runtime2);//true
            System.out.println("runtime1.totalMemory() = " + runtime1.totalMemory());//runtime1.totalMemory() = 128974848
            System.out.println("runtime2.freeMemory() = " + runtime2.freeMemory());//runtime2.freeMemory() = 122139384
    
            
    
    
        }
    }

     代码7

    System.out.println("-----------数组交换-----------");
            int[] arrTest ={10,12,20,30,40,50,60,70};
            System.out.println("数组交换前 = " + Arrays.toString(arrTest));
            int[] newArr = new int[arrTest.length];
            int index = 0;
            for (int i = arrTest.length-1; i >=0; i--) {
                newArr[index++]=arrTest[i];
            }
            arrTest=newArr;
            System.out.println("交换后 = " + Arrays.toString(arrTest));
    
            //或者
            for (int i = 0; i < arrTest.length/2; i++) {
                int temp = arrTest[i];
                arrTest[i]=arrTest[arrTest.length-1-i];
                arrTest[arrTest.length-1-i]=temp;
            }
            System.out.println("交换后 = " + Arrays.toString(arrTest));

    代码8

    //二分查找
    public class Demo5 {
        public static void main(String[] args) {
            int[] arr2 = {10,12,20,23,30,34,45,50,55,60};
    
            int index =binarySearch(arr2,55);
            if (index==-1){
                System.out.println("没有找到");
            }else {
                System.out.println("位置是:"+index);
            }
        }
    
        private static int binarySearch(int[] arr2,int ele){
            int startIndex=0;
            int endIndex=arr2.length-1;
            while (startIndex<=endIndex){
                int midIndex=(startIndex+endIndex)/2;
                int midValue=arr2[midIndex];
                if (ele>midValue){
                    startIndex=midIndex+1;
                }else if (ele<midValue){
                    endIndex=midIndex-1;
                }else {
                    return midIndex;
                }
            }
            return -1;
        }
    }

     代码9:

    import org.junit.Test;
    
    import java.util.Arrays;
    
    public class Demo6 {
        @Test
        public void test1(){
            //数组的扩容
            String[] arr1 = {"lin1","lin2","lin3"};
            String[] arr2 = new String[arr1.length*2];
            System.arraycopy(arr1,0,arr2,0,arr1.length);
            System.arraycopy(arr1,0,arr2,3,arr1.length);
            for(String s:arr2){
                System.out.println(s);
            }
        }
    
        @Test
        public void test2(){
            //数组的插入
            String[] arr1 = {"广坤","刘能","赵四","老徐","大拿"};
            int newIndex=2;
            String newValue="晓峰";
            String[] arr2 = new String[arr1.length+1];
            for (int i = 0; i < newIndex; i++) {
                arr2[i]=arr1[i];
            }
            arr2[newIndex]=newValue;
            System.arraycopy(arr1,newIndex,arr2,newIndex+1,arr1.length-newIndex);
            System.out.println(Arrays.toString(arr2));
        }
    
    //数组排序 @Test
    public void test3(){ int[] arr1 ={62,23,79,55,90,14,31,48,69,72,99,59}; System.out.println("排序前:"+Arrays.toString(arr1)); int temp; for (int i = 0; i <arr1.length ; i++) { for (int j = i+1; j <arr1.length ; j++) { if (arr1[i]>arr1[j]){ temp=arr1[i]; arr1[i]=arr1[j]; arr1[j]=temp; } System.out.println("排序中:"+Arrays.toString(arr1)); } } System.out.println("排序后:"+Arrays.toString(arr1)); } }

     代码10:

    class Person implements Comparable{
        String name;
        int age;
    
        public Person() {
        }
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        @Override
        public int compareTo(Object o) {
            Person p = (Person)o;
            return this.age-p.age;
        }
    }
    
    class Student {
    
        String name;
        int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    class CompareTest implements Comparator{
    
        @Override
        public int compare(Object o1, Object o2) {
            Student s1 = (Student)o1;
            Student s2 = (Student)o2;
            return s1.age-s2.age;
        }
    }
    
    
        @Test
        public void test4(){
            int[] arr1 = {82,23,62,78,92,41,69,88,};
            Arrays.sort(arr1);
            System.out.println(Arrays.toString(arr1));
            //[23, 41, 62, 69, 78, 82, 88, 92]
    
            int[] arr2 = {6,5,4,3,2,1};
            Arrays.sort(arr2,0,3);
            System.out.println(Arrays.toString(arr2));
            //[4, 5, 6, 3, 2, 1]
        }
    
        @Test
        public void test5(){
            Person p1 = new Person("张三",18);
            Person p2 = new Person("李四",17);
            Person p3 = new Person("王五",19);
            Person[] personArr ={p1,p2,p3};
            System.out.println("排序前:"+Arrays.toString(personArr));
            Arrays.sort(personArr);
            System.out.println("排序后:"+Arrays.toString(personArr));
            //排序前:[Person{name='张三', age=18}, Person{name='李四', age=17}, Person{name='王五', age=19}]
            //排序后:[Person{name='李四', age=17}, Person{name='张三', age=18}, Person{name='王五', age=19}]
    
            Student s1 = new Student("李白",23);
            Student s2 = new Student("白居易",30);
            Student s3 = new Student("苏东坡",12);
            Student[] studentArr = {s1,s2,s3};
            System.out.println("排序前:"+Arrays.toString(studentArr));
            CompareTest compareTest = new CompareTest();
            Arrays.sort(studentArr,compareTest);
            System.out.println("排序后:"+Arrays.toString(studentArr));
            //排序前:[Student{name='李白', age=23}, Student{name='白居易', age=30}, Student{name='苏东坡', age=12}]
            //排序后:[Student{name='苏东坡', age=12}, Student{name='李白', age=23}, Student{name='白居易', age=30}]
    
            int[] arr3 = new int[3];
            Arrays.fill(arr3,10);
            System.out.println(Arrays.toString(arr3));//[10, 10, 10]
    
            int[] arr4 = {1,2,3};
            Arrays.fill(arr4,10);
            System.out.println(Arrays.toString(arr4));//[10, 10, 10]
    
            int[] arr5 = new int[5];
            Arrays.fill(arr5,1,3,10);
            System.out.println(Arrays.toString(arr5));//[0, 10, 10, 0, 0]
    
    
            String[] s = {"赵匡胤","赵煦","赵祯","赵高"};
            String[] ss = Arrays.copyOf(s,2);
            ss[0]="赵构";
            System.out.println(Arrays.toString(ss));//[赵构, 赵煦]
    
            String[] sss = Arrays.copyOfRange(s,1,3);
            sss[0]="赵奢";
            System.out.println(Arrays.toString(sss));//[赵奢, 赵祯]
    
            int[][] arr6={{1,2},{2,3},{3,4}};
            System.out.println(Arrays.deepToString(arr6));//[[1, 2], [2, 3], [3, 4]]
    
            int[] arr7={1,2};
            int[] arr8={1,2};
            System.out.println(arr7==arr8);//false
            System.out.println(Arrays.equals(arr7,arr8));//true
    
            int[] arr9={82,34,55,12,48,99,78,51,82};
            Arrays.sort(arr9);
            System.out.println("arr9 = " + Arrays.toString(arr9));//arr9 = [12, 34, 48, 51, 55, 78, 82, 82, 99]
            int pos=Arrays.binarySearch(arr9,55);
            System.out.println("pos = " + pos);//4
    
        }

     代码11

        @Test
        public void test6(){
            String s1 ="hello";//字符串变量,指向"hello"在常量池中的位置
            String s2 ="world";//字符串变量,指向"hello"在常量池中的位置
            String s3 ="hello"+"world";//常量+常量=常量
            String s4 = "helloworld";//helloworld字符串常量
            String s5 = s1+s2;//变量+变量 在堆中
            String s6 = s1+"world";//变量+常量
            String s7 = new String("helloworld");
    
            System.out.println(s3==s4);//true
            System.out.println(s5==s4);//false
            System.out.println(s6==s5);//false
            System.out.println(s7==s4);//true
            //equals比较的是内容
            System.out.println(s7.equals(s4));//false
    
        }
    
        @Test
        public void test7(){
            final String s1 ="hello";//常量
            final String s2 ="world";//常量
            String s3 ="hello"+"world";//常量+常量=常量
            String s4 = "helloworld";//helloworld字符串常量
            String s5 = s1+s2;//常量+常量
            String s6 = s1+"world";//常量+常量
            String s8 = "hello";
            String s7 = new String("hello");
    
            System.out.println(s3 == s4);//true
            System.out.println(s4 == s5);//true
            System.out.println(s3 == s5);//true
            System.out.println(s6 == s5);//true
            System.out.println(s7 == s8);//false
            //intern()方法把数据直接存到常量池
            System.out.println(s7.intern() == s8);//true
        }

    代码12

  • 相关阅读:
    〖Linux〗Kubuntu设置打开应用时就只在打开时的工作区显示
    〖Linux〗Kubuntu, the application 'Google Chrome' has requested to open the wallet 'kdewallet'解决方法
    unity, dll is not allowed to be included or could not be found
    android check box 自定义图片
    unity, ios skin crash
    unity, Collider2D.bounds的一个坑
    unity, ContentSizeFitter立即生效
    类里的通用成员函数应声明为static
    unity, Gizmos.DrawMesh一个坑
    直线切割凹多边形
  • 原文地址:https://www.cnblogs.com/hbxZJ/p/15746983.html
Copyright © 2011-2022 走看看