第13周作业集 更改

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

package zuoyeshiyi;

import java.util.*;
public class tmyi {

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

第13周作业集 更改_第1张图片

 

题目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 tm1=new HashMap();
		HashMap tm2=new HashMap();
		HashMap tm3=new HashMap();
		HashMap tm4=new HashMap();

		for(int x=0;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 set=m.keySet();
	Iterator 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;

	}
}

第13周作业集 更改_第2张图片

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(第13周作业集 更改)