第13周作业集

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

源代码:

SetOpt.java

package Tym;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SetOpt {
    @SuppressWarnings("unchecked")
    public List intersect(List l1, List l2) { 
        @SuppressWarnings("unchecked")
        List list = new ArrayList(Arrays.asList(new Object[l1.size()])); 
        Collections.copy(list, l1); 
        list.retainAll(l2); 
        return list; 
    }

    public List union(List l1, List l2) { 
        List list = new ArrayList(Arrays.asList(new Object[l1.size()])); 
        Collections.copy(list, l1); 
        list.addAll(l2); 
        return list; 
    }
}

Test.java

package Tym;

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

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SetOpt opt=new SetOpt();
        List l1 = new ArrayList();
        l1.add("chen");
        l1.add("wang");
        l1.add("liu");
        l1.add("zhang");
        System.out.println("l1中储存的内容" + l1);
        List l2 = new ArrayList();
        l2.add("chen");
        l2.add("hu");
        l2.add("zhang");
        System.out.println("l2中储存的内容" + l2);
        List intersectList = opt.intersect(l1, l2); 
        System.out.println("交集:"); 
        for (int i = 0; i < intersectList.size(); i++) { 
            System.out.print(intersectList.get(i) + " "); 
        } 
        List unionList = opt.union(l1, l2);
        System.out.println(); 
        System.out.println("并集:"); 
        for (int j = 0; j < unionList.size(); j++) { 
            System.out.print(unionList.get(j) + " "); 
            
        }
        System.out.println();
        Set set = new HashSet();
            ArrayList newList = new ArrayList();
            set.addAll(unionList);
            newList.addAll(set);
            System.out.println("去重后两个线性表的并集为:" + newList);        
    }
}

输出结果:

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

源代码:

package Tym;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


public class TongJi {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("请输入一行字符串:");
        String str = sc.nextLine();
        HashMap al=new HashMap();
        HashMap bl=new HashMap();        HashMap cl=new HashMap();        char[] arr = str.toCharArray();
        int a = 0;//大写字母
        int b = 0;//小写字母
        int c = 0;//数字
        for (int i = 0; i < arr.length; i++) {
            char ch = arr[i];
            if (ch >= 'A' && ch <= 'Z') {
                a++;
                 al.put(i,arr[i]+",");
                continue;
            }
            if (ch >= 'a' && ch <= 'z') {
                b++;
                bl.put(i,arr[i]+",");
                continue;
            }
            if (ch >= '0' && ch <= '9') {
                c++;
                cl.put(i,arr[i]+",");
                continue;
            }
        }
        System.out.print("大写字母———共"+a+"个,"+"分别为"+"");
        Set set=al.entrySet();
        Iterator tao=set.iterator();
        while(tao.hasNext()){
            Map.Entry y1=(Map.Entry)tao.next();
            System.out.print(y1.getValue());
        }
        System.out.println("");
        System.out.print("小写字母———共"+b+"个,"+"分别为"+"");
        Set set1=bl.entrySet();
        Iterator ming=set1.iterator();
        while(ming.hasNext()){
            Map.Entry y2=(Map.Entry)ming.next();
            System.out.print(y2.getValue());
           }
        System.out.println("");
        System.out.print("数字———共"+c+"个,"+"分别为"+"");
        Set set2=cl.entrySet();
        Iterator ming1=set2.iterator();
        while(ming1.hasNext()){
            Map.Entry y3=(Map.Entry)ming1.next();
            System.out.print(y3.getValue());
            }
        
}}

运行结果:

posted @ 2019-11-29 19:44  随风而逝1996  阅读(119)  评论(0编辑  收藏  举报