现在我有两个阵列。一个是带有单词的字符串数组。和一个计数数组,其中包含相应的计数。下面我把它们打印在一起......
前:
gladiator 8
yamaha 6
gladiator 4
bike 6
brake 4
long 4
time 6
gladiator 35
yamaha 39
bike 17
gladiator 16
ss 19
type 16
yamaha 21
gladiator 15
india 8
yamaha 17
bike 7
gladiator 15
shine 9
yamaha 9
bike 8
gladiator 14
india 8
yamaha 18
bike 7
gladiator 9
所以现在我有第一个“角斗士”“8”然后“角斗士”“4”......等...... 最后我想把它当作“角斗士”“12”......就像明智的
答案 0 :(得分:1)
String.split(" ")
Integer.parseInt(array[1])
array[0]
。答案 1 :(得分:1)
您可以解析文件并将名称和长度存储在地图中。
Map <String,Integer> wordMap = new HashMap<String,Integer>() ;
然后遍历地图并找到最大计数。 与最大计数对应的键将是单词。
public class CountWord {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map <String,Integer> wordMap = new HashMap<String,Integer>() ;
int maxCount = 0;
int currentCount = 0;
wordMap.put("Yamaha", new Integer(6));
wordMap.put("Bat", new Integer(3));
wordMap.put("Honda", new Integer(5));
wordMap.put("Book", new Integer(4));
wordMap.put("CutiePie", new Integer(8));
wordMap.put("Project", new Integer(7));
Collection countList = wordMap.values();
Iterator itr = countList.iterator();
while(itr.hasNext()){
currentCount = ((Integer)itr.next()).intValue();
if(maxCount < currentCount){
maxCount = currentCount;
}
}
for (Object key: wordMap.keySet()) {
if(((Integer)wordMap.get(key)).intValue() == maxCount){
System.out.println("The word is : "+key.toString());
}
}
}
}