类库使用案例分析
StringBuffer使用
定义一个StringBuffer类对象,然后通过append()方法向对象中添加26个小写字母,要求每次只添加一次,共添加26次,然后按照逆序的方式输出,并且可以删除前五个字符;
本操作主要是训练StringBuffer类中的处理方法,因为StringBuffer的主要特点是允许修改。
public class String_Buffer_Use {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
for(int x='a'; x<='z';x++) {
buf.append((char)x);
}
buf.reverse().delete(0, 5); //反转删除
System.out.println(buf);
}
}
因为StringBuffer是允许修改的,而String内容不允许修改,现在的程序是一个单线程的开发,无需考虑并发访问问题。
随机数组
利用Random类产生5个1~30之间的随机整数。
import java.util.Arrays;
import java.util.Random;
class NumberFactory{
private static Random random =new Random();
/**
* 通过随机数生成一个数组的内容,该内容不包括0
* @param len 要开辟数组大小
* @return 包含随机数字的数组
*/
public static int [] create(int len) {
int data [] = new int [len];
int foot = 0 ;
while(foot< data.length) {
int num = random.nextInt(30);
if(num != 0) {
data[foot++] = num;
}
}
return data;
}
}
public class Random_shuzu_use {
public static void main(String[] args) {
int result [] = NumberFactory.create(5);
System.out.println(Arrays.toString(result));
}
}
Email验证
输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
package support_package_use;
class Validator{
//验证程序类
private Validator() {
}
public static boolean isEmail(String email) {
if(email == null|| "".equals(email)) {
return false;
}
String regex = "\\w+@\\w+\\.\\w+";
return email.matches(regex);
}
}
public class Email_valid_use {
public static void main(String[] args) {
if(args.length != 1) {
System.exit(1); //系统退出
}
String email = args[0];
if(Validator.isEmail(email)) {
System.out.println("有效");
}else {
System.out.println("无效");
}
}
}
扔硬币
用0~1随机数模拟扔硬币实验,统计1000次之后正反面次数;
import java.util.Random;
class Coin{
private int front; //正面
private int back; //背面
private Random random = new Random();
/**
* 扔硬币处理
* @param num 执行次数
*/
public void throwCoin(int num) {
for(int x=0;x<num;x++) {
int temp = random.nextInt(2);
if(temp ==0) {
this.front ++;
}else {
this.back++;
}
}
}
public int getFront() {
return front;
}
public int getBack() {
return back;
}
}
public class Random_Use {
public static void main(String[] args) {
Coin coin = new Coin();
coin.throwCoin(1000);
System.out.println("front:"+ coin.getFront());
System.out.println("back:"+ coin.getBack());
}
}
IP验证
编写正则表达式,判断一个给定的是否是一个合法的IP地址。
IP组成:
- 第一位的内容无、1、2。
- 后面的内容可以0-9;
- 第三位的内容0~9。
class Validators{
public static boolean validateIP(String ip) {
if(ip == null || "".equals(ip)) {
return false;
}
String regex = "([12]?[0-9]?[0-9]\\.){3}[12]?[0-9]?[0-9]";
if(ip.matches(regex)) {
String result[] = ip.split("\\.");
for(int x=0; x<result.length;x++) {
int temp = Integer.parseInt(result[x]);
if(temp > 255) {
return false;
}
}
} else {
return false;
}
return true;
}
}
public class Ip_Valid_Use {
public static void main(String[] args) {
String str = "192.168.1.1";
System.out.println(Validators.validateIP(str));
}
}
HTML拆分
给定下面的HTML代码,进行拆分:
<font face="Arial,Serif" size="+2" color=red>
拆分结果face Arial,Serif
,size +2
,color red
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HTML_split_use {
public static void main(String[] args) {
String str = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">";
String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\"" ;
Matcher matcher = Pattern.compile(regex).matcher(str);
while(matcher.find()) {
String temp = matcher.group(0);
String result[] = temp.split("=");
System.out.println(result[0]+"\t"+ result[1].replaceAll("\"",""));
}
}
}
国家代码
编写程序实现国际化应用,从命令行输入国家的代号,例如,1表示中国、2表示美国,然后根据代号不同调用冉的资源文件显示。
实现 通过Locale类的对象来指定区域,随后利用ResourceBundle类加载资源文件。
import java.util.Locale;
import java.util.ResourceBundle;
class Message{
public static final int CHINA=1;
public static final int USA=2;
private static final String key = "info";
private static final String BASENAME ="Messages";
public String getMessage(int num) {
Locale loc = this.getLocal(num);
if(loc == null) {
return "Nothing";
}else {
return ResourceBundle.getBundle(BASENAME, loc).getString(key);
}
}
private Locale getLocal(int num) {
switch(num) {
case CHINA:
return new Locale("zh","CN");
case USA:
return new Locale("en","US");
default:
return null;
}
}
}
public class Country_Use {
public static void main(String[] args) {
if(args.length != 1) {
System.exit(1);
}
int choose = Integer.parseInt(args[0]);
System.out.println(new Message().getMessage(choose));
}
}
学生信息比较
按照“姓名:年龄:成绩|姓名:年龄:成绩”的格式等你故意字符串“张三:21:98|李四:22:89|王五:20:70”,要求将每组值分别保存在Student对象中并排序:成绩>年龄;
import java.util.Arrays;
class Student implements Comparable<Student>{
private String name;
private int age;
private double score;
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]\n";
}
@Override
public int compareTo(Student o) {
if(this.score< o.score) {
return 1;
}else if(this.score > o.score){
return -1;
} else {
return this.age - o.age;
}
}
}
public class Student_Use {
public static void main(String[] args) {
String input = "lyz:21:98|zky:22:89|zpb:20:70";
String result[] = input.split("\\|");
Student students[] = new Student[result.length];
for (int x=0; x<result.length;x++) {
String temp[] = result[x].split(":");
students[x] = new Student(temp[0], Integer.parseInt(temp[1]), Double.parseDouble(temp[2]));
}
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}