Random类
java.util.Random类
以种子为基础,编译的算法来生成随机数(Random如何生成随机数 )
//无参构造 默认种子每次都不一样
//不同的种子构造的Random对象生成的随机数是不同的
Random r=new Random();//创建Random对象
Random r1=new Random();
System.out.println(r.nextInt());//范围全int范围
System.out.println(r.nextInt(100));//范围0-100
System.out.println(r1.nextInt());
相同的种子构造的Random对象生成的随机数都是相同的
Random r=new Random(2);//创建Random对象
Random r1=new Random(2);
System.out.println(r.nextInt());//范围全int范围
System.out.println(r1.nextInt());
当种子值固定时,伪随机
Random ran=new Random(2);
System.out.println(ran.nextInt(100));
第一次生成随机数后,再次点击,生成的随机数与上次相同
如果需要加种子,种子可以用时间的毫秒数
String类
String类位于java.lang包中
使用String存储字符串
String类的常用构造方法 以String做参数,无参
以byte[] 数组做参数 以char[] 数组做参数
String s="abc";//直接赋值
String s1=new String("abc");
String s3=new String();//空字符串
String s2="abc";
String s4 = new String(s);
char[] c={'a','b','c'};
String s5 = String.valueOf(c);//常用于接收数组
System.out.println(s == s1);
System.out.println(s == s2);
System.out.println(s == s4);
System.out.println(s == s5);
equals( )方法,比较存储在两个字符串对象的内容是否一致
==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象
String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1.equals(s2));//true
System.out.println(s1 == s2);//false 共创建了3个对象
//栈存放变量 堆存放对象 字符串池 String s="hello";
String与char[]
String类型底层存储是char类型的常量数组
String类本身用final修饰,不能被继承
如果方法上有final修饰,不能被重写
char[] c={'a','b','c'};
System.out.println(c);//打印的是值 不是地址
String s=new String(c);//以char[]为参数
System.out.println(s);
String s1=String.valueOf(c);//char数组转为String
System.out.println(s1.toCharArray());//String转为char数组
其他常用方法
length()方法,确定字符串的长度
equalsIgnoreCase()方法
toLowerCase()方法
toUpperCase()方法
String s="abcdEFG";
String s1="abcdefg";
System.out.println(s.length());
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s.equalsIgnoreCase(s1));
trim() 前后去掉空格
String s=" hello , niha o , ";
System.out.println(s);
System.out.println(s.trim());//左右去空格
substring()
//截取子字符串
String s="abcdEFG";
System.out.println(s.substring(1));
System.out.println(s.substring(1, 4));//前闭后开[1,4)
//查找特定字符在字符串中出现的次数
System.out.print("请输入一个字符串:");
Scanner input=new Scanner(System.in);
String s=input.next();
System.out.println("输入要查找的字符:");
String want=input.next();
String[] s1=new String[s.length()];//将s中所有字符拆分出来 数组长度就是字符串的长度
int count=0;
for (int i = 0; i < s1.length; i++) {
s1[i]=s.substring(i,i+1);//substring(0,1)取出来的是第一个字符 依次截取
if (s1[i].equals(want)){
count++;
}
}
System.out.println(want+"在"+s+"中出现的次数是:"+count);
indexOf()第一次出现的位置
lastIndexOf()最后一次出现的位置
String s="hello,my name is zzzok";
System.out.println(s.indexOf("o"));
System.out.println(s.lastIndexOf("o"));
System.out.println(s.indexOf("b"));//没有找到返回-1
String s1=s.substring(1, 5);
System.out.println(s.indexOf(s1));
System.out.println(s.charAt(1));//返回index为1的字符
拼接和替代
String s="abc";
System.out.println(s.concat("aaaaa"));//相当于+
System.out.println(s.replace('a', 'z'));
split()字符串拆分
String words="长亭外 古道边 芳草碧连天 晚风 拂柳 笛声残";
String[] printWords = new String[100];
System.out.println("原歌词:");
System.out.println(words);
printWords= words.split(" ");//以空格拆分 返回一个字符串数组
System.out.println("拆分后:");
for (String printWord : printWords) {
System.out.println(printWord);
}
String的所有操作不会影响字符串本身,影响的是字符串的副本
StringBuffer
//StringBuffer是线程安全的类型 StringBuilder是单线程类型
StringBuffer s = new StringBuffer();
s.append("abc,");
s.append(123);
char[] c={',','h','e','l','l','o'};
s.append(c);
s.append(12.4f);
System.out.println(s);//重写了toString方法
System.out.println(s.length());
String s1=s.toString();//buffer转成字符串
System.out.println(s1);
StringBuffer ss= new StringBuffer(s1);//使用构造方法将Sring转成buffer
// ss.replace(1,10,"");//截掉
ss.replace(1,12,"zzz");
System.out.println(ss);
String 底层是一个不可变的char数组常量
变更时会先新建String对象 再新建char数组 再重新赋值 对性能消耗大
StringBuffer是可变的char数组常量 适合在多线程场景下使用 相对于StringBuilder
如果在长度以内 变更时不会再次新建StringBuffer对象和char[]对象
如果超过长度 变更时不会新建tringBuffer对象 只是新建char[]对象
对性能消耗非常小
String 安全性最高 性能最低
builder 安全性最低 性能最高
buffer 折中 在实际工作中,只要可能涉及多线程,就是使用buffer