44クラスライブラリのユースケース分析

クラスライブラリのユースケース分析

StringBufferの使用

  StringBufferクラスオブジェクトを定義し、append()メソッドを使用してオブジェクトに26個の小文字を追加します。一度に1回だけ、合計26回追加してから、逆の順序で出力し、最初の文字を追加する必要があります。 5文字を削除できます。
この操作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クラスを使用して、1〜30の5つのランダムな整数を生成します。

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));
	}
}

メールによる確認

  メールアドレスを入力し、正規表現を使用してメールアドレスが正しいことを確認します。

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アドレスであるかどうかを判別します。
IP構成:

  • 最初の桁の内容はnone、1、2です。
  • 次の内容は0〜9です。
  • 3位の内容は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,Serifsize +2color 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)。次に、コードに従ってRanのリソースファイル表示を呼び出します。
実装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オブジェクトに保存し、並べ替えます:Grade> Age;

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));
	}
}

おすすめ

転載: blog.csdn.net/MARVEL_3000/article/details/113029274