实用类
首先实用类能够干什么,能够定义并使用枚举类型,需要掌握:包装类及装箱、拆箱概念,会使用Math类进行数学运算,使用Random类获取随机数,会使用String操作字符串。
首先我们为什么要用枚举
public class Student{
public String sex;
}
Student stu=new Student();
stu.sex=“你好”;
如果有人故意捣乱在sex里输入你好或者其他的东西 ,这样就虽然不会报错但是也不严谨;所以我们要用枚举,(枚举指由一组固定的常量组成的类型),
枚举类型安全,易于输入,代码清晰!也就是我们最好用枚举这个方法;
枚举的格式是
[Modifier] enum enumName{
enumContantName1
[,enumConstantName2…[;]]
//[field,method]
}
比如我们自己学习java的时候有三个学习阶段,那么我们可以用枚举定义:
我们选择新建 enum
public enum Time {
L1, L2, L3
}
然后
public class ClassTest {
public static void main(String[] args) {
Time time = Time.L3;
switch (time) {
case L1:
System.out.println(“实用类1”);
break;
case L2:
System.out.println(“实用类2”);
break;
case L3:
System.out.println(“实用类3”);
break;
}
}
}
这样类型安全,易于输入,代码清晰。
包装类
把基本类型数据转换为对象
每个基本类型在java.lang包中都有一个相应的包装类
它的作用提供了一系列实用的方法,集合不允许存放基本数据类型数据,存放数字时,要用包装类型
所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
public Type(type value)
如:Integer i=new Integer(1);
除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
public Type(String value)
如:Integer i=new Integer(“123”);
我们也需要注意Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
基本类型->包装类
Integer intValue = Integer.valueOf(21);
字符串->包装类
Integer intValue = Integer.valueOf(“21”);
关于装箱和拆箱
装箱:基本类型转换为包装类的对象
int intValue =5;
拆箱:包装类对象转换为基本类型的值
Integer intObject = 5;
Math类
Math类的常用的数学运算方法:
Math.abs(-3.5); //返回3.5
Math.max(2.5, 90.5);//返回90.5
int random = (int) (Math.random() * 10); //生成一个0-9之间的随机数
Random类
生成随机数的其他方式
比如:
Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
int num=rand.nextInt(10);//返回下一个伪随机数整 型的
System.out.println(“第”+(i+1)+“个随机数是:”+num);
}
用同一个种子值来初始化两个Random 对象Random rand=new Random(num); ,然后用每个对象调用相同的方法,得到的随机数也是相同的
String类
String类计算字符串的长度、比较字符串、连接字符串、提取字符串
String类提供了length()方法,确定字符串的长度
提供了equals()方法,比较存储在两个字符串对象的内容是否一致
==和equals()有什么区别呢?:
==判断两个字符串在内存中的地址,即判断是否是同一个字符串对象,equals():检查组成字符串内容的字符是否完全一致
字符串比较的其他方法:
使用equalsIgnoreCase()方法
使用toLowerCase()方法
使用toUpperCase()方法
字符串连接
方法1:使用“+”
方法2:使用String类的concat()方法
字符串常用提取方法
案例1:
public class Test2 {
public static void main(String[] args) {
// 判断帐号123@qq.com 帐号是否一个邮箱帐号(邮箱的规则是 必须有@ .
// 符号,@符号在.符号的前面)
String str = “123@qq.com”;
int index = str.lastIndexOf(".");
int index1 = str.lastIndexOf("@");
if (index = = -1 || index1 = = -1) {
System.out.println(“邮箱格式不正确”);
return;
}
if (index < index1) {
System.out.println(“邮箱格式不正确”);
return;
}
String t = str.substring(index);
System.out.println(“截取内容:” + t);
if (!t.equals(".com")) {
System.out.println(“邮箱格式错误!”);
} else {
System.out.println(“格式正确!!”);
}
}
private static void p1() {
String a = "hello.java";
int index = a.lastIndexOf(".");
if (index == -1) {
System.out.println("改文件不是java文件");
} else {
String str = a.substring(index);
System.out.println("截取内容是" + str);
if (str.equals(".java")) {
System.out.println("是一个java文件");
} else {
System.out.println("不是一个java文件");
}
}
}
}
案例2将歌词中带有空格部分的拆分一下
public class Test2 {
public static void main(String[] args) {
System.out.println(“原歌词格式”);
String str=“长亭外 古道边 芳草碧连天 晚风扶 柳笛声残 夕阳山外山”;
System.out.println(str);
String []s=str.split(" ");
System.out.println();
System.out.println(“拆分后的歌词格式”);
for(String temp:s){
System.out.println(temp);
}
}
}
案例3:查找输入的字符串里有多少个你想要查找的字符数;
public class Test4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“请输入一个字符串:”);
String str = input.next();
System.out.println(“请输入你要查找的字符:”);
String str1 = input.next();
char cha = str.charAt(0);
int count=0;
for (int i = 0; i < str.length(); i++) {
char temp2 = str.charAt(i);
String temp3 = temp2 + “”;
if (temp3.equals(str1)) {
count++;
}
}
System.out.println(str1+“字符出现的次数:”+count);
}
}