判断大小写数字个数,取交集和并集

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

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

 

package Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class test16 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Question1();
        //Question2("XieXiaFei678");
    }
    public static void Question1() {
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        list1.add("chen");
        list1.add("wang");
        list1.add("liu");
        list1.add("zhang");
        list2.add("chen");
        list2.add("hu");
        list2.add("zhang");
        List<String> list3 = new ArrayList<String>();
        Iterator<String> it = list1.iterator();
        while(it.hasNext()) {
            String str = it.next();
            if(list2.contains(str)) {
                list3.add(str);
            }
            if(!(list2.contains(str))) {
                list2.add(str);
            }
        }
        System.out.println("交集:"+list3.toString());
        System.out.println("并集:"+list2.toString());
    }
    public static void Question2(String str) {
        List<Character> digit = new ArrayList<Character>();
        List<Character> lower = new ArrayList<Character>();
        List<Character> upper = new ArrayList<Character>();
        char []chs = str.toCharArray();
        for(int i =0;i<chs.length;i++) {
            if(Character.isDigit(chs[i])) {
                digit.add(chs[i]);
            }
            if(Character.isLowerCase(chs[i])) {
                lower.add(chs[i]);
            }
            if(Character.isUpperCase(chs[i])) {
                upper.add(chs[i]);
            }
        }
        if(digit.size()!=0) {
            System.out.println("数字的个数是:"+digit.size()+"分别是:"+digit.toString());
        }else {
            System.out.println("数字的个数是0");
        }
        if(upper.size()!=0) {
            System.out.println("大写字母的个数是:"+upper.size()+"分别是:"+upper.toString());
        }else {
            System.out.println("大写字母的个数是0");
        }
        if(lower.size()!=0) {
            System.out.println("小写字母的个数是:"+lower.size()+"分别是:"+lower.toString());
        }else {
            System.out.println("小写的个数是0");
        }
    }
}

    public static void Question3(String str) {
        Map<Integer,Character> digit = new HashMap<Integer,Character>();
        Map<Integer,Character> upper = new HashMap<Integer,Character>();
        Map<Integer,Character> lower = new HashMap<Integer,Character>();
     char []chs = str.toCharArray();
        for(int i =0;i<chs.length;i++) {
            if(Character.isDigit(chs[i])) {
                digit.put(i,chs[i]);
            }
            if(Character.isLowerCase(chs[i])) {
                lower.put(i,chs[i]);
            }
            if(Character.isUpperCase(chs[i])) {
                upper.put(i,chs[i]);
            }
        }
        Collection<Character> digitColl = digit.values();
        Collection<Character> upperColl = upper.values();
        Collection<Character> lowerColl = lower.values();
        if(digitColl.size()!=0) {
            System.out.println("数字的个数是:"+digitColl.size()+"分别是:"+digitColl.toString());
        }else {
            System.out.println("数字的个数是0");
        }
        if(upperColl.size()!=0) {
            System.out.println("大写字母的个数是:"+upperColl.size()+"分别是:"+upperColl.toString());
        }else {
            System.out.println("大写字母的个数是0");
        }
        if(lowerColl.size()!=0) {
            System.out.println("小写字母的个数是:"+lowerColl.size()+"分别是:"+lowerColl.toString());
        }else {
            System.out.println("小写的个数是0");
        }
    }

posted @ 2019-11-26 16:13  James_Harden  阅读(410)  评论(0编辑  收藏  举报