第13周作业集

题目1:创建两个线性表,分别存储{“chen”,“wang”,“liu”,“zhang”}和{“chen”,“hu”,“zhang”},求这两个线性表的交集和并集。

一、代码

package uu;
import java.util.HashSet;

public class inter {
    public static void main(String[] args) {
        HashSet hs1=new HashSet();
        HashSet hs2=new HashSet();
        HashSet hs3=new HashSet();
        hs1.add("chen");
        hs1.add("wang");
        hs1.add("liu");
        hs1.add("zhang");
        System.out.println("hs1的内容是:"+hs1);
        hs2.add("chen");
        hs2.add("hu");
        hs2.add("zhang");
        System.out.println("hs2的内容是:"+hs2);
        hs3.clear();           //把对象hs3的内容清除
        hs3.addAll(hs1);       //把hs1的内容添加到hs3里
        hs1.retainAll(hs2);    //求交集
        hs3.addAll(hs2);       //求并集
        System.out.println("hs1和hs2的交集是:"+hs1);
        System.out.println("hs1和hs2的并集是:"+hs3);

    }

}

二、运行结果

第13周作业集_第1张图片

 

题目2:编写一个应用程序,输入一个字符串,该串至少由数字、大写字母和小写字母三种字符中的一种构成,如“123”、“a23”、“56aD”、“DLd”、“wq”、“SSS”、“4NA20”,对输入内容进行分析,统计每一种字符的个数,并将该个数和每种字符分别输出显示。如:输入内容为“34Ah5yWj”,则输出结果为:数字——共3个,分别为3,4,5;小写字母——共3个,分别为h,y,j;大写字母——共2个,分别为A,W。

 一、代码

package uu;
import java.util.*;
import java.util.Set;
public class ccc {

    public static void main(String[] args) {
        int UpperCase = 0, LowerCase = 0, digit = 0;
        System.out.println("请输入字符串:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        Map map=new HashMap();
        HashMap hm1=new HashMap();
        HashMap hm2=new HashMap();
        HashMap hm3=new HashMap();
        char[] ch=str.toCharArray();
        
        for (int i = 0; i < ch.length; i++) {
        if (Character.isDigit(ch[i])) {
            digit++;
            hm1.put(i,ch[i]+",");
        } else if (ch[i] >= 'A' && ch[i] <= 'Z') {
            UpperCase++;
            hm2.put(i,ch[i]+",");
        } else if (ch[i] >= 'a' && ch[i] <= 'z') {
            LowerCase++;
            hm3.put(i,ch[i]+",");
        }
    }
    System.out.print("数字共"+digit+"个,"+"分别为"+" ");
    Set set=hm1.entrySet();
    Iterator t=set.iterator();
    while(t.hasNext()){
        Map.Entry me=(Map.Entry)t.next();
        System.out.print(me.getValue());
    }
    System.out.print("大写字母共"+UpperCase+"个,"+"分别为"+" ");
    Set set1=hm2.entrySet();
    Iterator t1=set1.iterator();
    while(t1.hasNext()){
        Map.Entry me1=(Map.Entry)t1.next();
        System.out.print(me1.getValue());
    }
    System.out.print("小写字母共"+LowerCase+"个,"+"分别为"+" ");
    Set set2=hm3.entrySet();
    Iterator t2=set2.iterator();
    while(t2.hasNext()){
        Map.Entry me2=(Map.Entry)t2.next();
        System.out.print(me2.getValue());
    }
}
}

二、运行结果

你可能感兴趣的:(第13周作业集)