案例:
定义一个StringBuffer类对象,然后通过append()方法像对象添加26个小写字母,要求每次只能添加一个,共添加26次,然后按照逆序方式输出,然后删除前5个字符。
public class JavaApiDemo {
public static void main(String[] args) throws Exception{
StringBuffer buffer=new StringBuffer();
for (int i='a',l='z';i<=l;i++){
buffer.append((char)i);
}
System.out.println("正序输出结果:"+buffer.toString());//正序输出结果:abcdefghijklmnopqrstuvwxyz
buffer.reverse();//反转处理
System.out.println("逆序输出结果:"+buffer.toString());//逆序输出结果:zyxwvutsrqponmlkjihgfedcba
buffer.delete(0,5);
System.out.println("删除前5个字符后输出结果:"+buffer.toString());//删除前5个字符后输出结果:utsrqponmlkjihgfedcba
}
}
案例:利用Random类产生5个1~30之间(包括1和30)的随机整数。
public class JavaApiDemo {
public static void main(String[] args) throws Exception{
int count=5;//数字个数
int min=1,max=30;//最小取值1,最大取值为30
//而我们知道Random.nextInt(n)取值范围是[0,n),则需要在范围[0,n+1-min)取值后加上最小值
int[] data=new int[count];
Random random=new Random();
for (int i=0;i<count;i++){
data[i]=min+random.nextInt(max+1-min);
}
System.out.println("生成结果为:"+Arrays.toString(data));//生成结果为:[28, 3, 24, 4, 20]
}
}
案例:利用Random生成36选7彩票(不包含0,数字不能重复)
public class JavaApiDemo {
public static void main(String[] args) throws Exception {
int data[] = new int[7];//开辟7个大小的空间
Random random = new Random();
int foot = 0;//操作data脚标
while (foot < 7) {//选择7个数字
int num = random.nextInt(37);//生成一个小于37的自然数
if (isUse(num, data)) {//判断是否可用
data[foot++] = num;//保存数据
}
}
Arrays.sort(data);
for (int d : data) {
System.out.print(d + "、");
}
}
/**
* 判断传入的数字是否为0以及是否在数组中已存在
*
* @param num 要判断的数字
* @param temp 已存在的数据
* @return 如果该数字不是0并且在数字中不存在返回true, 否则返回false
*/
public static boolean isUse(int num, int[] temp) {
if (num == 0) {
return false;
}
for (int t : temp) {
if (t == num) {
return false;
}
}
return true;
}
}
案例:编写程序,用0~1之间的随机数来模拟扔硬币实验,统计扔1000次后出现正、反面的次数并输出。
import java.util.Random;
public class JavaApiDemo {
public static void main(String[] args) throws Exception {
CoinThrow coinThrow=new CoinThrow();
coinThrow.throwCoin(1000);
System.out.println(coinThrow.getResult());//总共投掷1000次,正面497次,反面503次
}
}
class CoinThrow {//模拟硬币投掷操作
private int front;//正面
private int back;//反面
private int times=0;
private Random random = new Random();
/**
* 投掷硬币的处理
* @param times 投掷次数
*/
public void throwCoin(int times) {
this.times=times;
for (int i = 0; i < times; i++) {
int num = random.nextInt(2);
if (num == 0) {
back++;//反面
} else {
front++;//正面
}
}
}
public String getResult(){
return String.format("总共投掷%s次,正面%s次,反面%s次",times,front,back);
}
}
案例:使用正则表达式验证Email地址格式是否正确。
- email的用户名可以由(字母、数字、)所组成(不能以开头);
- email的域名可以由(字母、数字、_、-)所组成;
- email的域名后缀必须是:.cn、.com、.net、,com.cn、.gov;
public class JavaApiDemo {
public static void main(String[] args) throws Exception {
String email="jhflr999@qq.comxcn";
String regex="[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|net|com\\.cn|gov)";
System.out.println(email.matches(regex));
}
}
案例:使用正则表达式验证IP地址格式是否正确。
public class JavaApiDemo {
public static void main(String[] args) throws Exception {
String ip="255.255.255.255";
String regex="^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$";
System.out.println(ip.matches(regex));
}
}
案例:给定Html代码,并对内容进行拆分。
<input class="js-course-allowAnonymousPreview" data-user-id="1282916" type="hidden" value="0">
拆分结果:
class:js-course-allowAnonymousPreview
data-user-id:1282916
type:hidden
value:0
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaApiDemo {
public static void main(String[] args) throws Exception {
String html = "<input class=\"js-course-allowAnonymousPreview\" data-user-id=\"1282916\" type=\"hidden\" value=\"0\">";
String regex = "\\s([\\w\\-]+)=\"([a-zA-Z0-9\\-]+)\"";
Matcher m = Pattern.compile(regex).matcher(html);
while (m.find()) {
System.out.println(m.group(1) + ":" + m.group(2));
}
/**
* class:js-course-allowAnonymousPreview
* data-user-id:1282916
* type:hidden
* value:0
*/
}
}