第13周作业集

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

代码

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


public class jihe {

    /**
     * @param args
     */
    public static void main(String[] args) {
      ArrayList<String> s1=new ArrayList<String>();
      ArrayList<String> s2=new ArrayList<String>();
      ArrayList<String> s3=new ArrayList<String>();
      ArrayList<String> s4=new ArrayList<String>();
      s1.add("chen");
      s1.add("wang");
      s1.add("liu");
      s1.add("zhang");
      s2.add("chen");
      s2.add("hu");
      s2.add("zhang");
      System.out.println("两个线性表");
      s3.clear();
      s3.addAll(s1);
      s3.retainAll(s2);            //去除两个集合不相同的元素
     System.out.println("交集为"+s3);
      s4.clear();
      s4.addAll(s1);
      s4.addAll(s2);
      Set bingj=new HashSet<String>();//去重
      bingj.addAll(s4);
     System.out.println("并集为:" + bingj);
      
    }

}

结果

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

 代码(属实不会,借鉴同学)

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class c {
    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;//a数字 计数   b 小写计数  c 大写计数
        for(int i=0;i<str.length();i++){
            String g = str.substring(i, i+1);//摘串
            if(g.matches(number)){//判数字        
                if(hm.get("数字")==null){
                    hm.put("数字", g);
                }else{
                    hm.put("数字", hm.get("数字")+","+g);
                }
                a++;
            }
            if(g.matches(lower)){//判小写字母
                if(hm.get("小写字母")==null){
                    hm.put("小写字母", g);
                }else{
                    hm.put("小写字母", hm.get("小写字母")+","+g);
                }
                b++;
            }                
            if(g.matches(capital)){//判大写字母
                if(hm.get("大写字母")==null){
                    hm.put("大写字母", g);
                }else{
                    hm.put("大写字母", hm.get("大写字母")+","+g);
                }
                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/12yy/p/11953835.html