前言
map,set,matches,split,StringBuffer,StringTokenizer的运用
提示:以下是本篇文章正文内容,下面案例可供参考
一.map
package chapter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class examplemap {
public static void main(String[] args) {
//遍历Map 第一种方式
Map<String, Integer> map1 = new HashMap<String, Integer>();
map1.put("jack", 20);
map1.put("rose", 18);
map1.put("lucy", 17);
map1.put("java", 25);
//通过 map1.keySet() 获取key 通过key 找到value
for (String key : map1.keySet()) {
Integer value = map1.get(key);
System.out.println("key : "+key+" value : "+value);
}
//第二种
for(Map.Entry<String, Integer>entry:map1.entrySet()){
System.out.println("key : "+entry.getKey()+" value : "+entry.getValue());
}
//第三种
Set<String> key=map1.keySet();
for(String s:key) {
System.out.println(s+" "+map1.get(s));
}
}
}
二.set
代码如下(示例):
package chapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class exampleset {
public static void main(String[] args) {
Set<String> set=new HashSet<String>();
set.add("12345");
set.add("78910");
List<String> list=new ArrayList<String>();
list.add("学院");
set.addAll(list);
set.addAll(list = new ArrayList<String>());
System.out.println(set.size());
for(String i:set){
System.out.println(i);
}
Iterator<String> iter=set.iterator();
while(iter.hasNext()){
String i=iter.next();
System.out.println(i);
}
//String[] array=set.toArray(new String[0]);
String []s=new String[0];
String[] array=set.toArray(s);
System.out.println(Arrays.toString(array));
}
}
三.matches运用
代码如下(示例):
package chapter;
import java.util.Scanner;
//判断键盘输入是否全由英文字母组成
public class examplematches {
public static void main(String[] args) {
String regex="[a-zA-Z]+";
Scanner ae=new Scanner(System.in);
String str=ae.nextLine();
if(str.matches(regex)) {
System.out.println(str+"中的字母都是英文字母");
}
else {
System.out.println("no");
}
}
}
四.split
package chapter;
import java.util.Scanner;
//通过split分离字符串
public class examplesplit {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
String str=reader.nextLine();
String regex="[\\s\\d\\p{Punct}]+";
String words[]=str.split(regex);
for(String i:words) {
System.out.println(i);
}
}
}
五.StringBuffer
package chapter;
public class examplestringbuffer {
public static void main(String[] args) {
StringBuffer str=new StringBuffer();
str.append("大家好");
System.out.println("str"+str);
System.out.println("length"+str.length());
System.out.println("capacity:"+str.capacity());
str.setCharAt(0, 'w');
str.setCharAt(1, 'e');
System.out.println(str);
str.insert(2, " are all");
int index=str.indexOf("好");
str.replace(index, str.length(), " right");
System.out.println(str);
}
}
六.StringTokenizer
package chapter;
import java.util.StringTokenizer;
public class examplestringtokenizer {
public static void main(String[] args) {
String s="you are welcome(thank you),nice to meet you";
StringTokenizer fenxi=new StringTokenizer(s,"() ,");
int number=fenxi.countTokens();
while(fenxi.hasMoreTokens()) {
String str=fenxi.nextToken();
System.out.print(str+" ");
}
System.out.println("共有单词:"+number+"个");
}
}
# 七.BigInterget
package chapter;
import java.math.BigInteger;
public class example03 {
public static void main(String[] args) {
BigInteger i=new BigInteger("11111111111111");//创建一个BigInteger
BigInteger j=new BigInteger("22222222222222");
System.out.println(i.add(j));//加
System.out.println(i.subtract(j));//减
System.out.println(i.multiply(j));//乘
System.out.println(i.divide(j));//除
}
}