• 第13次作业集


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

    Test1.java

    package c;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    public class Test1 {
    public static void main(String[] args) {
    List<String> list=new ArrayList<String>();//创建2个线性表
    list.add("chen");
    list.add("wang");
    list.add("liu");
    list.add("zhang");
    List<String> list1=new ArrayList<String>();//创建2个线性表
    list1.add("chen");
    list1.add("hu");
    list1.add("zhang");
    List<String> list3=new ArrayList<String>();
    list3.addAll(list); //将list中 的元素加入到list3中
    list3.retainAll(list1);//删除包含在list1的元素之外的全部元素
    System.out.println("交集是"+list3);
    HashSet<String> s=new HashSet<String>();
    s.addAll(list);//将list元素加到s中
    s.addAll(list1);//将list1元素加到s中
    System.out.println("并集是"+s);
    }
    }

    运行结果

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

    Test2.java

    package c;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    public class Test3 {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("请输入一个字符串");
    String s=sc.nextLine();
    char a[]=s.toCharArray();//把字符串变成单个字符放入数组a中
    int xiao=0,da=0,shu=0;
    Map<String,Integer> m=new HashMap<String,Integer>();//创建HashMap对象
    m.put("小写字母", xiao);
    m.put("大写字母", da);
    m.put("数字", shu);
    StringBuffer str1=new StringBuffer();//创建字符串对象
    StringBuffer str2=new StringBuffer();
    StringBuffer str3=new StringBuffer();
    for(int i=0;i<a.length;i++){//for(循环遍历数组)
    if(a[i]>='0'&&a[i]<='9'){
    xiao=m.get("数字");
    str1.insert(shu, a[i]);
    m.put("数字", ++shu);
    }
    if(a[i]>='A'&&a[i]<='Z'){//判断是否为小写字符
    xiao=m.get("大写字母");
    str2.insert(da, a[i]);//把大写字符加入到数组str2中
    m.put("大写字母", ++da);//累加器自加
    }
    if(a[i]>='a'&&a[i]<='z'){ //判断是否为小写字符
    xiao=m.get("小写字母");
    str3.insert(xiao, a[i]);//把小写字符加入到数组str3中
    m.put("小写字母", ++xiao);//累加器自加
    }
    }
    System.out.println("数字"+m.get("数字")+"个,分别是:"+str1);
    System.out.println("大写字母"+m.get("大写字母")+"个,分别是:"+str2);
    System.out.println("小写字母"+m.get("小写字母")+"个,分别是:"+str3);

    }
    }

     运行结果

  • 相关阅读:
    ActionBarSherlock的使用——(一)配置
    一个炫酷的Actionbar效果
    Action Bar详解(二)
    Android L中的RecyclerView 、CardView 、Palette的使用
    Material Theme
    Material Design UI Widgets
    Material Design Get Started
    递归获取目录中所有文件
    Django ModelForm 模型表单组件
    使用 Python 统计中文字符的数量
  • 原文地址:https://www.cnblogs.com/LJTQ/p/11963502.html
Copyright © 2020-2023  润新知