第13周作业集

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

代码:

package org.ccut.pack_12;

import java.util.ArrayList;

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

    public static void main(String[] args) {

        ArrayList<String> l1 = new ArrayList<String>();
        ArrayList<String> l2 = new ArrayList<String>();
        ArrayList<String> union ;
        ArrayList<String> intersection =new ArrayList<String>();
        l1.add("chen");
        l1.add("wang");
        l1.add("liu");
        l1.add("zhang");

        l2.add("chen");
        l2.add("hu");
        l2.add("zhang");

        // 输出两个集合
        System.out.println(l1);
        System.out.println(l2);

        // 求交集
        union = l1.size() > l2.size() ? new ArrayList<>(l1) : new ArrayList<>(l2);
        union.retainAll(l1.size() < l2.size() ? l1 : l2);
        System.out.println("交集:"+union);
        // 求并集
        ArrayList<String> t1=new ArrayList<>(l1);
        ArrayList<String> t2=new ArrayList<>(l2);
        t1.removeAll(l2);
        t2.removeAll(l1);
        intersection.addAll(union);
        intersection.addAll(t1);
        intersection.addAll(t2);
        System.out.println("并集:"+intersection);

    }


}

 

 

运行结果:

 

 

 

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

 

用Map实现的代码:

package org.ccut.pack_13.f1;

import java.util.*;

public class StatisicalCharacterByMap {

    public static void main(String[] args) {
        //键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        //将字符串赋值给变量s
        String s = sc.nextLine();
        //定义一个char类型数组,并把字符串导入这个数组
        char[] arr = s.toCharArray();
        //创建一个引用对象,HashMap集合
        HashMap<Character, Integer> hm = new HashMap<>();
        //使用增强for循环把数组内容添加到集合,
        for (char c : arr) {
            //再添加的时候,判断是否重复,并对出现次数进行统计
            hm.put(c, hm.containsKey(c) ? hm.get(c) + 1 : 1);
        }
        //输出集合
        System.out.println(hm);
        //对这个集合进行遍历
        StringBuilder sb = new StringBuilder();
        int ln = 0, la = 0, lA = 0;
        for (Map.Entry<Character, Integer> en : hm.entrySet()) {
            char key = en.getKey();
            int value = en.getValue();
            if (key >= '0' && key <= '9') {
                ln += value;
            } else if (key >= 'a' && key <= 'z') {
                la += value;
            } else if (key >= 'A' && key <= 'Z') {
                lA += value;
            }

        }
        sb.append("数字").append("出现").append(ln).append("次").append(",");
        sb.append("小写字母").append("出现").append(la).append("次").append(",");
        sb.append("大写字母").append("出现").append(lA).append("次").append(",");
        System.out.println(sb.substring(0, sb.length() - 1));
    }

}

 

 

运行结果:

 

 

 用ArrayList实现的代码:

package org.ccut.pack_13;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

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

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        for (int i = 0; i < str.length(); i++) {
            list.add(str.charAt(i) + "");
        }
        System.out.println("集合list:" + list);

        // 统计字符
        int a = 0, b = 0, c = 0;
        ArrayList<String> la = new ArrayList<>();
        ArrayList<String> lb = new ArrayList<>();
        ArrayList<String> lc = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String ch = list.get(i);
            if (ch.charAt(0) > '0' && ch.charAt(0) < '9') {
                a++;
                la.add(ch);
            } else if (ch.charAt(0) > 'a' && ch.charAt(0) < 'z') {
                b++;
                lb.add(ch);
            } else if (ch.charAt(0) > 'A' && ch.charAt(0) < 'Z') {
                c++;
                lc.add(ch);
            }
        }
        System.out.println("数字:" + a + "个,分别是:" + la);
        System.out.println("小写字母:" + b + "个,分别是:" + lb);
        System.out.println("大写字母:" + c + "个,分别是:" + lc);

        // 附加:求字符个数
        while (list.size() > 0) {
            String t = list.get(0);
            int n = 0;
            while (true) {
                if (list.contains(t)) {
                    list.remove(t);
                    n++;
                } else {
                    System.out.println(t + ":" + n);
                    break;
                }
            }
        }
    }
}

 

 

运行结果

 

 

 

 


posted @ 2019-11-27 09:18  20194680刘厚飞  阅读(229)  评论(0编辑  收藏  举报