java实验九选六已完成(3)

2019-03-27 19:24

for(int i=0;i<10;i++){ //为集合加入元素 int anyNumber;

Random random=new Random();

anyNumber=random.nextInt(10)+1; hashSetA.add(anyNumber);

anyNumber=random.nextInt(10)+1; hashSetB.add(anyNumber); }

Set mySet=new Set(hashSetA,hashSetB);

System.out.printf(\集合A为:\ mySet.print( hashSetA);

System.out.printf(\集合B为:\ mySet.print( hashSetB);

System.out.printf(\和B的并集为:\ mySet.print(mySet.getSum());

System.out.printf(\和B的交集为:\ mySet.print(mySet.getMixed());

System.out.printf(\集合A对B的差集为:\

mySet.print(mySet.getDifference(hashSetA, hashSetB));

System.out.printf(\集合B对A的差集为:\

mySet.print(mySet.getDifference(hashSetB, hashSetA)); } }

//可进行并、交、差集运算--------------- class Set{

private HashSet h1=new HashSet(); private HashSet h2=new HashSet();

private HashSet hSum=new HashSet(); //并集 private HashSet hMixed=new HashSet(); //交集

private HashSet hDifference=new HashSet(); //h差集 public Set(HashSet h1,HashSet h2){ this.h1=h1; this.h2=h2;

for (Object k:h1) { hSum.add(k);

}

for(Object k:h2){ boolean b;

b=hSum.add(k); if(!b){

hMixed.add(k); } } }

public HashSet getSum(){ return hSum; }

public HashSet getMixed(){ return hMixed; }

public HashSet getDifference (HashSet h1,HashSet h2){ this.h1=h1; this.h2=h2;

for(Object k:h1){ boolean b;

b=hMixed.add(k); if(b){

hDifference.add(k); } }

return hDifference; }

public void print(HashSet h){ int i=1;

for(Object it: h){

System.out.print(it+\

if(i==0)

System.out.println(\ i++; }

System.out.println(\ }

}

三————————————————————————————————— ////三.给定一个英文文本文件(>100MB),统计其中的各种不同的英文单词的个数,并将统计结果存入一个文件中。 import java.io.BufferedWriter; import java.io.File;

import java.io.FileWriter;

import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Map;

import java.util.Scanner;

import java.util.StringTokenizer; public class Exam4_3 {

static String fileName; static String filePath;

public static void main(String[] args) throws Exception{ Scanner scan=new Scanner(System.in);

System.out.println(\输入读取文件的路径:\ filePath=scan.nextLine();

System.out.println(\输入要读取的文件名:\ fileName=scan.nextLine();

Map map=new HashMap(); ReadText readText=new ReadText(fileName,filePath,map); map=readText.getWords();

System.out.println(\输入要生成文件的路径:\ filePath=scan.nextLine();

System.out.println(\输入要生成文件的文件名:\

fileName=scan.nextLine();

System.out.println(fileName+\文件生成\

File text=new File(filePath+fileName); text.createNewFile();

FileWriter fileWriter=new FileWriter(text);

BufferedWriter bufferedWriter=new BufferedWriter(fileWriter); for(String k:map.keySet()){

bufferedWriter.write(k+\个\ bufferedWriter.newLine();

System.out.println(k+\个\ }

bufferedWriter.flush(); fileWriter.close();

bufferedWriter.close(); } }

class ReadText{

String fileName; String filePath;

Map map;

public ReadText(String fileName,String filePath,Map map){ this.fileName=fileName; this.filePath=filePath; this.map=map; }

public Map getWords()throws Exception{ String str;

File text=new File(filePath,fileName);

System.out.println(\文件是否存在:\ RandomAccessFile randomAccessFile=new RandomAccessFile(text,\

while((str=randomAccessFile.readLine())!=null){

StringTokenizer analyse=new StringTokenizer(str,\\\n \\t ; : ? \\\

while(analyse.hasMoreTokens()){

String string=analyse.nextToken();

if(!map.containsKey(string)) map.put(string,\ else

map.put(string,String.valueOf((Integer.valueOf(map.get(string))+1))); }

analyse=null; }

randomAccessFile.close();

return map; } }


java实验九选六已完成(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017年度师德师风培训材料(3)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: