第13周作业集
题目一:
创建两个线性表,分别存储{“chen”,“wang”,“liu”,“zhang”}和{“chen”,“hu”,“zhang”},求这两个线性表的交集和并集。
一、代码
1 /**
2 * @author
3 */
4 package ccut;
5
6 import java.util.*;
7 public class Test {//创建一个测试类
8 public static void main(String[] args) {
9 ArrayList<String> str1=new ArrayList<String>(); //声明四个ArrayList对象
10 ArrayList<String> str2=new ArrayList<String>();
11 ArrayList<String> str3=new ArrayList<String>();
12 ArrayList<String> str4=new ArrayList<String>();
13 str1.add("chen");//存入元素
14 str1.add("wang");
15 str1.add("liu");
16 str1.add("zhang");
17 str2.add("chen");
18 str2.add("hu");
19 str2.add("zhang");
20 str3.addAll(str1);
21 str3.retainAll(str2);
22 str4.addAll(str1);
23 str4.addAll(str2);
24 Set set=new HashSet<String>();
25 set.addAll(str4);
26 System.out.println("集合一:"+str1);//输出结果
27 System.out.println("集合二:"+str2);
28 System.out.println("两个集合的交集为:"+str3);
29 System.out.println("两个集合的并集为:"+set);
30 }
31 }
二、输出结果

题目二
编写一个应用程序,输入一个字符串,该串至少由数字、大写字母和小写字母三种字符中的一种构成,如“123”、“a23”、“56aD”、“DLd”、“wq”、“SSS”、“4NA20”,对输入内容进行分析,统计每一种字符的个数,并将该个数和每种字符分别输出显示。如:输入内容为“34Ah5yWj”,则输出结果为:数字——共3个,分别为3,4,5;小写字母——共3个,分别为h,y,j;大写字母——共2个,分别为A,W。
一、代码
1 /**
2 * @author
3 */
4 package ccut;
5 import java.util.*;
6
7 public class Test01 {
8 public static void main(String[] args) {
9 int UpperCase = 0, LowerCase = 0, digit = 0;// 大写小写和数字
10 Scanner reader = new Scanner(System.in);
11 System.out.println("输入内容为:");
12 String str = reader.nextLine();
13 HashMap al=new HashMap();
14 HashMap bl=new HashMap();//
15 HashMap cl=new HashMap();//
16 char[] ch = str.toCharArray();
17 for (int a = 0; a < ch.length; a++) {
18 if (Character.isDigit(ch[a])) {
19 digit++;
20 al.put(a,ch[a]+",");
21 } else if (ch[a] >= 'B' && ch[a] <= 'Z') {
22 UpperCase++;
23 bl.put(a,ch[a]+",");
24 } else if (ch[a] >= 'b' && ch[a] <= 'z') {
25 LowerCase++;
26 cl.put(a,ch[a]+",");
27 }
28 }
29 System.out.print("数字共"+digit+"个,"+"分别为"+" ");
30 Set set=al.entrySet();
31 Iterator lu=set.iterator();
32 while(lu.hasNext()){
33 Map.Entry me=(Map.Entry)lu.next();
34 System.out.print(me.getValue());
35 }
36 System.out.print("大写字母共"+UpperCase+"个,"+"分别为"+" ");
37 Set set1=bl.entrySet();
38 Iterator lu1=set1.iterator();
39 while(lu1.hasNext()){
40 Map.Entry me1=(Map.Entry)lu1.next();
41 System.out.print(me1.getValue());
42 }
43 System.out.print("小写字母共"+LowerCase+"个,"+"分别为"+" ");
44 Set set2=cl.entrySet();
45 Iterator lu2=set2.iterator();
46 while(lu2.hasNext()){
47 Map.Entry me2=(Map.Entry)lu2.next();
48 System.out.print(me2.getValue());
49 }
50 }
51 }
二、输出结果


浙公网安备 33010602011771号