面向对象 (11)求交并集 判断字符形式

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

一、源程序

/**两个线性表的交集和并集
 * 1个方法
 */
package cn.edu.ccut.w1125t1;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList<String> s1 = new ArrayList<String>();//创建两个线性表
        ArrayList<String> s2 = new ArrayList<String>();
        s1.add("chen");//储值
        s1.add("wang");
        s1.add("liu");
        s1.add("zhang");
        s2.add("chen");
        s2.add("hu");
        s2.add("zhang");
        ArrayList<String> B = new ArrayList<String>();//创建存储并集线性表B
        B.addAll(s1);//存入s1全部元素
        int i;//循环变量i
        for(i=0;i<3;i++){//遍历s2线性表
            if(B.contains(s2.get(i))){//已有则向下遍历
                continue;
            }else{//无则添加
                B.add(s2.get(i));
            }
        }
        System.out.print("交集是"+B+"
");
        ArrayList<String> J = new ArrayList<String>();//创建存储交集线性表J
        for(i=0;i<3;i++){//遍历s2线性表
            if(s1.contains(s2.get(i))){//共有则添加
                J.add(s2.get(i));
            }else{
                continue;
            }
        }
        System.out.print("并集是"+J+"
");
    }
}

二、成功界面截图

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

一、源程序

/**输入一个字符串分析统计每一种字符的个数,并将该个数和每种字符分别输出显示。
 * 1个方法
 */
package cn.edu.ccut.w1125t2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
    public static void main(String[] args) {
        System.out.print("请输入一个字符串(该串至少由数字、大写字母和小写字母三种字符中的一种构成)
");
        Scanner reader = new Scanner(System.in);
        String str = reader.nextLine();//获取串
        String number = "\d";
        String lower = "[a-z]";
        String capital = "[A-Z]";//定义判断格式正则
        HashMap<String, String> hm = new HashMap<String, String>();//建散列映射
        int a=0,b=0,c=0;//定义个数计数器
        for(int i=0;i<str.length();i++){//遍历串
            String word = str.substring(i, i+1);//摘串
            if(word.matches(number)){//判数字        
                if(hm.get("数字")==null){
                    hm.put("数字", word);
                }else{
                    hm.put("数字", hm.get("数字")+","+word);//同键值追加
                }
                a++;
            }
            if(word.matches(lower)){//判小写字母
                if(hm.get("小写字母")==null){
                    hm.put("小写字母", word);
                }else{
                    hm.put("小写字母", hm.get("小写字母")+","+word);
                }
                b++;
            }                
            if(word.matches(capital)){//判大写字母
                if(hm.get("大写字母")==null){
                    hm.put("大写字母", word);
                }else{
                    hm.put("大写字母", hm.get("大写字母")+","+word);
                }
                c++;
            }
        }
        Set set = hm.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("——共"+a+"个,");
            }else if(me.getKey().equals("小写字母")){
                System.out.print("——共"+b+"个,");
            }else if(me.getKey().equals("大写字母")){
                System.out.print("——共"+c+"个,");
            }
            System.out.println("分别是:"+me.getValue());    
        }
    }
}

二、成功界面截图

原文地址:https://www.cnblogs.com/wangxiangyue/p/11929639.html