-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeekWord3.java
More file actions
executable file
·109 lines (85 loc) · 2.46 KB
/
SeekWord3.java
File metadata and controls
executable file
·109 lines (85 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;
public class SeekWord3{
public static void main(String[] args)throws Exception{
RandomAccessFile rf = new RandomAccessFile("hello2.txt", "rw");
String temp= rf.readLine();
//基本思想是:先把所有元素都存到一个集合中,最好是元素不可重复的集合。
//然后在把所有元素都取出来,放到一个字符串组中,
//然后在把文件遍历一遍,遍历的过程中计数
//基本思想是:先把所有元素都存到一个集合中,最好是元素不可重复的集合。
HashSet hs = new HashSet();
while(temp!=null){
temp=temp.replace(".","");
temp=temp.replace(",","");
//temp=temp.replace("\r","");
String[] str=temp.split(" ");
for (int i=0;i<str.length ;i++ ) {
hs.add(str[i]);
}
temp= rf.readLine();
}//以上步骤,所有单词已经存到集合中
//把集合中的单词转存到字符串组中
String[] data= new String[hs.size()];
int[] num= new int[hs.size()];
int x=0;
for (Iterator it=hs.iterator();it.hasNext() ; ){
data[x++]=(String)it.next();
}
//集合中的单词转存到字符串组中
rf.seek(0);
temp= rf.readLine();
while(temp!=null){
temp=temp.replace(".","");
temp=temp.replace(",","");
//temp=temp.replace("\r","");
String[] str=temp.split(" ");
for (int i=0;i<str.length ;i++ ) {
for (int j=0;j<data.length ;j++ ) {
if (data[j].equals(str[i])) {
num[j]++;
break;
}
}
}
temp= rf.readLine();
}//统计每个单词的频次结束
int x1=0;
for (int i=0;i<num.length ;i++ ) {
if (num[i]==1) {
x1++;
}
}
System.out.println("总共有"+num.length+"个单词.");
System.out.println("有"+x1+"个单词只出现一次。");
System.out.println("各单词出现的次数为:");
int[] sortedArray =(int[])num.clone();
bubbleSort(sortedArray);
for (int i=0;i<sortedArray.length ;i++ ) {
for (int j=0;j<num.length ;j++ ) {
if (num[j]==sortedArray[i]&&!data[j].equals("")) {
System.out.println(data[j]+":"+num[j]);
num[j]=-1;
}
}
}
}
public static void bubbleSort(int[] numbers)
{
int temp = 0;
int size = numbers.length;
for(int i = 0 ; i < size-1; i ++)
{
for(int j = 0 ;j < size-1-i ; j++)
{
if(numbers[j] < numbers[j+1])
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
}
}