第13周作业集

一.题目1

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

二.源代码1

Text.java

import java.util.HashSet;
public class Test {
    public static void main(String[] args) {
        HashSet Z = new HashSet();    //创建线性表
        Z.add("chen");                 //添加元素
        Z.add("wang");
        Z.add("liu");
        Z.add("zhang");
        HashSet S = new HashSet();   
        S.add("chen");
        S.add("hu");
        S.add("zhang");
        HashSet X = new HashSet();
        X.addAll(Z);     //将z线性表中元素加入s线性表中
        X.retainAll(S);    //剔除与表s中不相同的元素    
        System.out.println("交集:"+X);
        HashSet R = new HashSet();
        R.addAll(Z);      
        R.addAll(S);     //哈希表唯一
        System.out.println("并集:"+R);
    }
 
}

三.运行结果

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

 

一.题目2

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

二.源代码2

Test.java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Test {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String str = reader.nextLine();
        System.out.println("输入字符串");
        HashMap H = new HashMap();   //创建HashMap
        int a=0;        
        int b=0;       
        int c=0;        
        for(int i=0;i){
            String str1 = str.substring(i, i+1);   //接受一个新字符串
            if(str1.matches( "\\d")){             //判断该位是否为数字
                if(H.get("数字")==null)
                    H.put("数字", str1);
                else
                    H.put("数字", H.get("数字")+","+str1);       
                a++;
            }
            if(str1.matches("[a-z]")){              //判断是否为小写字母
                if(H.get("小写字母")==null){
                    H.put("小写字母", str1);
                }else{
                    H.put("小写字母", H.get("小写字母")+","+str1);
                }
                c++;
            }               
            if(str1.matches("[A-Z]")){//是否为大写字母
                if(H.get("大写字母")==null){           
                    H.put("大写字母", str1);
                }else{
                    H.put("大写字母", H.get("大写字母")+","+str1);
                }
                c++;
            }
        }
        Set set = H.entrySet();        //返回的集合
        Iterator it = set.iterator();  
        while(it.hasNext()){         //用迭代器遍历
            Map.Entry   M = (Map.Entry)it.next();
            System.out.print(M.getKey());
            if(M.getKey().equals("数字")){
                System.out.print("——共"+a+"个,");
            }else if(M.getKey().equals("小写字母")){
                System.out.print("——共"+c+"个,");
            }else if(M.getKey().equals("大写字母")){
                System.out.print("——共"+b+"个,");
            }
            System.out.println("分别为:"+M.getValue());   
        }
    }
}

三.运行结果

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

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