张迪d

导航

统计

第13周作业集 更改

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

package zuoyeshiyi;

import java.util.*;
public class tmyi {

public static void main(String[] args) {
		List<String> list1 = new ArrayList<String>(Arrays.asList("chen","wang","liu","zhang"));
		List<String> list2 = new ArrayList<String>(Arrays.asList("chen","hu","zhang"));	
		List<String> l1 = new ArrayList<String>(list1);
		List<String> l2 = new ArrayList<String>(list2);		
		l2.removeAll(l1);
		l2.addAll(l1);
		System.out.println("并集"+l2);	 
		List<String> l3 = new ArrayList<String>(list1);
		List<String> l4 = new ArrayList<String>(list2);	
		System.out.println("交集"+jiaoji(l3,l4));
	}
	public static List jiaoji(List l3,List l4) {		
		List<String> newl = new ArrayList<String>();
		for(Iterator<String> it=l3.iterator();it.hasNext();) {	
			String s=it.next();
			if(l4.contains(s)) {
				newl.add(s);
			}
		}
		return newl;
	}
}

 

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

 
package zy;
import java.util.*;

//import com.sun.deploy.util.StringUtils;


public class zyss {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sca=new Scanner(System.in);
		String s=sca.nextLine();
		char [] ch=s.toCharArray();
		int bigcount = 0;
		int smallcount = 0;
		int count = 0;
		int qitacount = 0;

		HashMap<Integer,Character> tm1=new HashMap<Integer,Character>();
		HashMap<Integer,Character> tm2=new HashMap<Integer,Character>();
		HashMap<Integer,Character> tm3=new HashMap<Integer,Character>();
		HashMap<Integer,Character> tm4=new HashMap<Integer,Character>();

		for(int x=0;x<ch.length;x++) {
			
			if(ch[x]>='A' && ch[x]<= 'Z') {
				bigcount++;
				tm1.put(bigcount,ch[x]);
			}else if(ch[x] >= 'a' && ch[x] <= 'z') {
				smallcount++;
				tm2.put(smallcount,ch[x]);
			}else if(ch[x] >= '0' && ch[x] <= '9') {
				count++;
				tm3.put(count, ch[x]);
			}else {
				qitacount++;
				tm4.put(qitacount, ch[x]);
			}	
		}
		
		System.out.print("大写字母——共"+bigcount+"个,分别为");
		aaa(tm1);
		System.out.print("小写字母——共"+smallcount+"个,分别为");
		aaa(tm2);
		System.out.print("数字——共"+count+"个,分别为");
		aaa(tm3);
		System.out.print("其他字符——共"+qitacount+"个,分别为");
		aaa(tm4);
	}
	public static char aaa(Map m) {
	Set<Integer> set=m.keySet();
	Iterator<Integer> it=set.iterator();
	char[]cr=new char[m.size()];
	int i=-1;
	while(it.hasNext()) {
		i++;
		Integer key=it.next();
		Character value = (Character) m.get(key);
		cr[i]=value;	
		if(i!=cr.length-1) {
			System.out.print(cr[i]+",");
		}
		else {
			System.out.println(cr[i]);
		}
	}
	return 0;

	}
}

 

 

 

 

 

 

 

 

 

posted on 2019-11-26 15:13  张迪d  阅读(...)  评论(...)    收藏