### 实用类

- 枚举

枚举(Enum)是一种特殊的数据类型,既是一个类同时又比普通的类多了一些约束,正是因为有了这些约束,所以枚举具有简洁、安全、方便等特点。可以理解,枚举的值被约束到了一个特定的取值范围,只能取该范围内的值,超过这个范围,无法取值。

枚举是指由一组常量组成的类型,指定了一个取值区间,我们只能从该区间中取值。

```java
public enum WeekE {
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
}
```

枚举中的每一个常量都对应一个枚举实例,只不过表示的含义不同。

```java
package com.southwind.test;

public final class Week extends Enum {
    public static final Week MONDAY;
    public static final Week TUESDAY;
    public static final Week WEDNESDAY;
    public static final Week THURSDAY;
    public static final Week FRIDAY;
    public static final Week SATURDAY;
    public static final Week SUNDAY;
    private static final Week VALUES[];
    
    private Week(String s,int i) {
        super(s,i);
    }
    
    static {
        MONDAY = new Week("MONDAY",0);
        TUESDAY = new Week("TUESDAY",1);
        WEDNESDAY = new Week("WEDNESDAY",2);
        THURSDAY = new Week("THURSDAY",3);
        FRIDAY = new Week("FRIDAY",4);
        SATURDAY = new Week("SATURDAY",5);
        SUNDAY = new Week("SUNDAY",6);
        VALUES[] = (new Week[] {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY});
    }
    
    public static Week[] values() {
        return VALUES;
    }
    
    public static Week valueOf(String s) {
        return Enum.valueOf(s);
    }
    
}
```



### Math

Math 类为开发者提供了一系列的数学方法,同时还提供了两个静态常量E(自然对数的底数)和PI(圆周率),Math 类中的方法都是静态的,可以直接通过类名访问。

```java
public class Test2 {
    public static void main(String[] args) {
        System.out.println("常量E:"+Math.E);
        System.out.println("常量PI:"+Math.PI);
        System.out.println("9的平方根是:"+Math.sqrt(9));
        System.out.println("8的立方根是:"+Math.cbrt(8));
        System.out.println("2的3次方:"+Math.pow(2, 3));
        System.out.println("较大值是:"+Math.max(6.3, 5.3));
        System.out.println("较小值是:"+Math.min(1, 2));
        System.out.println("-10.3的绝对值是:"+Math.abs(-10.3));
        System.out.println(Math.ceil(10.00000001));
        System.out.println(Math.floor(10.9999999));
        System.out.println(Math.round(5.4));
        System.out.println("-------------------");
        for(int i = 0; i < 20; i++) {
            System.out.println(Math.random());
        }
    }
}
```



### Random

Random 是用来产生一个随机数的类,并且可以任意指定一个区间,在此区间内产生一个随机数,常用方法如下。

public Random()                    创建一个无参的随机数构造器,使用系统时间作为默认种子

public Random(long seed)               使用long类型的种子创建一个随机数构造器

public boolean nextBoolean()         返回下一个随机数(truefalse),它的取值范围来自分布均匀的 booleanpublic double nextDouble()            随机生成一个double类型的数据

public float nextFloat()                随机生成一个float类型的数据

public int nextInt()                    随机生成一个int类型的数据

public long nextLong()                随机生成一个long类型的数据



```java
import java.util.Random;

public class Test3 {
    public static void main(String[] args) {
        Random random = new Random();
//        for(int i = 1; i <= 3;i++) {
//            boolean flag = random.nextBoolean();
//            System.out.println(flag);
//        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextDouble());
//        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextFloat());
//        }
        for(int i = 0; i< 10;i++) {
            System.out.println(random.nextInt());
        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextInt(10));
//        }
        for(int i = 0;i<10;i++) {
            System.out.println(random.nextLong());
        }
    }
}
```



### String

- String 实例化

String 类对象的实例化方式有两种,第1种是直接赋值的方式,第2种是通过构造函数创建实例化对象的方式。String 类有一个带参构造:public String(String value);将 String 类型的数据直接传入即可。

- String 常用的方法

  public String()                                    创建值为空的对象

  public String(String value)                           创建值为value的对象

  public String(char[] value)                           将一个char类型的数组转为字符串对象

  public String(char[] value,int offset,int count)         将一个指定范围的char型数组转为字符串对象

  public String(byte[] bytes)                           将一个byte类型的数组转为字符串对象

  public String(byte[] bytes,int offset,int count)         将一个指定范围的byte型数组转为字符串对象

  public int length()                                  获取字符串的长度

  public boolean isEmpty()                            判断字符串是否为空

  public char charAt(int index)                         获取字符串中指定位置的字符

  public byte[] getBytes()                            将字符串转为byte类型的数组

  public boolean equals(Object obj)                    判断两个字符串是否相等

  public boolean equalsIgnoreCase(Object obj)          判断两个字符串是否相等同时忽略大小写

  public int compareTo(String str)                    对两个字符串进行排序,比较大小

  public boolean startsWith(String str)                判断字符串是否以某个字符串开头

  public boolean endsWith(String str)                 判断字符串是否以某个字符串结尾

  public int hashCode()                            获取字符串的hash值

  public int indexOf(String str)                        从头开始查找指定字符串的位置

  public int indexOf(String str,int fromIndex)            从指定的位置开始查找字符串的位置

  public String substring(int index)                    截取字符串从指定位置开始到字符串结尾

  public String substring(int index,int end)            截取字符串在指定的区间内

  public String[] split(String str)                        用指定字符串对目标进行分割,返回数组

  

Test.java

public class Test {
    public static void main(String[] args) {
//        WeekE week = WeekE.MONDAY;
        WeekE[] weeks = WeekE.values();
        for(WeekE week:weeks) {
            System.out.println(week);
        }
        WeekE week = WeekE.valueOf("TUESDAY");
        System.out.println(week);
    }
}

 

Test2.java

public class Test2 {
    public static void main(String[] args) {
        System.out.println("常量E:"+Math.E);
        System.out.println("常量PI:"+Math.PI);
        System.out.println("9的平方根是:"+Math.sqrt(9));
        System.out.println("8的立方根是:"+Math.cbrt(8));
        System.out.println("2的3次方:"+Math.pow(2, 3));
        System.out.println("较大值是:"+Math.max(6.3, 5.3));
        System.out.println("较小值是:"+Math.min(1, 2));
        System.out.println("-10.3的绝对值是:"+Math.abs(-10.3));
        System.out.println(Math.ceil(10.00000001));
        System.out.println(Math.floor(10.9999999));
        System.out.println(Math.round(5.4));
        System.out.println("-------------------");
        for(int i = 0; i < 20; i++) {
            System.out.println(Math.random());
        }
    }
}

 

Test3.java

import java.util.Random;

public class Test3 {
    public static void main(String[] args) {
        Random random = new Random();
//        for(int i = 1; i <= 3;i++) {
//            boolean flag = random.nextBoolean();
//            System.out.println(flag);
//        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextDouble());
//        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextFloat());
//        }
        for(int i = 0; i< 10;i++) {
            System.out.println(random.nextInt());
        }
//        for(int i=0;i<10;i++) {
//            System.out.println(random.nextInt(10));
//        }
        for(int i = 0;i<10;i++) {
            System.out.println(random.nextLong());
        }
    }
}

 

Test4.java

public class Test4 {
    public static void main(String[] args) {
//        //第1种方式直接赋值
//        String str1 = "Hello";
//        //第2种方式构造函数
//        String str2 = new String("World");
//        System.out.println(str1);
//        System.out.println(str2);
//        String str1 = "Hello";
//        String str2 = "Hello";
//        System.out.println(str1 == str2);
//        String str3 = new String("World");
//        String str4 = new String("World");
//        System.out.println(str3 == str4);
//        System.out.println("*******************");
//        System.out.println(str1.equals(str2));
//        System.out.println(str3.equals(str4));
        
        String str1 = new String("Hello");
        String str2 = str1;
        System.out.println(str2 == str1);
        str1 += "World";
        System.out.println(str2 == str1);
        
        
    }
}

 

Week.java

public class Week {
    public static final int MONDAY = 0;
    public static final int TUESDAY = 1;
    public static final int WEDNESDAY = 2;
    public static final int THURSDAY = 3;
    public static final int FRIDAY = 4;
    public static final int SATURDAY = 5;
    public static final int SUNDAY = 6;
}

WeekE.java

public enum WeekE {
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
}

 

posted on 2019-07-11 22:07  HiJackykun  阅读(353)  评论(0编辑  收藏  举报