第13周作业集

一、题目

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

二、源程序

ListTest.java

package pac_5;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class ListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ArrayList<String> a1 = new ArrayList<String>();
        a1.add("chen");
        a1.add("wang");
        a1.add("liu");
        a1.add("zhang");
        ArrayList<String> a2 = new ArrayList<String>();
        a2.add("chen");
        a2.add("hu");
        a2.add("zhang");
        ArrayList<String> a3 = new ArrayList<String>();
        a3.addAll(a1);
        a3.retainAll(a2);
        System.out.println("线性表的交集是:" + a3);
        a3.addAll(a1);
        a3.addAll(a2);
        Set<String> set = new HashSet<String>();// 去除重复
        ArrayList<String> aa = new ArrayList<String>();
        set.addAll(a3);
        aa.addAll(set);
        System.out.println("线性表的并集是:" + aa);

    }

}

三、运行结果

 

 一、题目

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

二、源程序

MapTest.java

package pac_5;
import java.util.*;

public class MapTest {

    public static void main(String[] args) {
        System.out.println("输入字符串:");
        HashMap<String, String> M = new HashMap<String, String>();
        Scanner r = new Scanner(System.in);
        String str = r.nextLine();
        int length = str.length();    
        String c;
        int num = 0; 
        int numA = 0;
        int numa = 0;
        for(int i = 0 ; i < length ; i++){
            c = str.substring(i, i+1);
            if(c.matches("\\d")){       
                if(M.get("数字")==null){
                    M.put("数字", c);
                }
                else{
                    M.put("数字", M.get("数字")+","+c);
                }
                num++;
            }
            if(c.matches("[a-z]")){
                if(M.get("小写字母")==null){
                    M.put("小写字母", c);
                }
                else{
                    M.put("小写字母", M.get("小写字母")+","+c);
                }
                numa++;
            }                
            if(c.matches("[A-Z]")){
                if(M.get("大写字母")==null){
                    M.put("大写字母", c);
                }
                else{
                    M.put("大写字母", M.get("大写字母")+","+c);
                }
                numA++;
            }
        }
        Set set = M.entrySet();   
        Iterator it = set.iterator();
        while(it.hasNext()){
            Map.Entry me = (Map.Entry)it.next();
            System.out.print(me.getKey());
            if(me.getKey().equals("数字")){
                System.out.print("——共"+num+"个,");
            }else if(me.getKey().equals("小写字母")){
                System.out.print("——共"+numa+"个,");
            }else if(me.getKey().equals("大写字母")){
                System.out.print("——共"+numA+"个,");
            }
            System.out.println("分别为"+me.getValue());    
        }
    }
}

三、运行结果

 

 

posted @ 2019-12-01 22:44  20194628胡艳春  阅读(169)  评论(0编辑  收藏  举报