使用 HashMaps Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15211626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using HashMaps Java
提问by Ali
I'm writing a method that allows me to count how many times an element of type String shows up in a LinkedList of type Strings. my code shown below does not work. I keep getting index out of bounds in the line i commented on down below. Can't seem to find the bug
我正在编写一个方法,它允许我计算 String 类型的元素在 Strings 类型的 LinkedList 中出现的次数。我下面显示的代码不起作用。我在下面评论的那一行中一直让索引越界。好像找不到bug
public int findDuplicate (LinkedList<String> e) {
int j = 1;
LinkedList<String> test = e;
while (!test.isEmpty()){
test = e;
String value = test.pop();
//Screws up here when i = 6
for(int i =0; i<=test.size() && test.get(i)!=null; i++){
String value3 = test.get(i);
if(e.get(i).equals(value) && i<=test.size()){
String value2 = test.get(i);
j++;
String Duplicate = e.get(i);
e.remove(i);
}
}
System.out.println(value + " is listed " + j + " times");
}
return j;
}
using hashmaps.. still doesn't work
使用 hashmaps .. 仍然不起作用
public void findDuplicate (LinkedList e) {
Map<String,Integer> counts = new HashMap<String,Integer>();
while(!e.isEmpty()){
String value = e.pop();
for(int i =0; i<e.size(); i++){
counts.put(value, i);
}
}
System.out.println(counts.toString());
}
My code should go through the linked list find out how many times an element within the list appears and deletes duplicates from the list at the same time. Then prints the element and the number of times it appears in the list. I posted about this last night but didn't get a response yet. Sorry for the repost.
我的代码应该通过链接列表找出列表中的元素出现的次数并同时从列表中删除重复项。然后打印元素及其在列表中出现的次数。我昨晚发布了这个,但还没有得到回应。对不起,重新发布。
回答by rgettman
You are running off the end of the list. Change
你快跑到列表的末尾了。改变
for(int i =0; i<=test.size() && test.get(i)!=null; i++){
to
到
for(int i =0; i< test.size() && test.get(i)!=null; i++){
Valid indexes for a List
(or an array) are 0
through size() - 1
.
a List
(或数组)的有效索引是0
through size() - 1
。
回答by Mike Hogan
Regarding your hashmap example to count the duplicates:
关于您的哈希图示例以计算重复项:
@Test
public void countOccurrences() {
LinkedList<String> strings = new LinkedList<String>(){{
add("Fred");
add("Fred");
add("Joe");
add("Mary");
add("Mary");
add("Mary");
}};
Map<String,Integer> count = count(strings,new HashMap<String,Integer>());
System.out.println("count = " + count);
}
private Map<String, Integer> count(List<String> strings, Map<String, Integer> runningCount) {
if(strings.isEmpty()) {
return runningCount;
}
String current = strings.get(0);
int startingSize = strings.size();
while(strings.contains(current)) {
strings.remove(current);
}
runningCount.put(current, startingSize - strings.size());
return count(strings,runningCount);
}
If you want the original strings list preserved you could do
如果您希望保留原始字符串列表,您可以这样做
Map<String,Integer> count = count(new LinkedList<String>(strings),new HashMap<String,Integer>());
System.out.println("strings = " + strings);
System.out.println("count = " + count);
回答by bsautner
Check out google's guava collections which has a perfect class for maintaining a map and getting a count:
查看谷歌的番石榴集合,它有一个完美的类来维护地图和获取计数:
https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap
https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap
Multiset<String> wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(words);
// now we can use wordsMultiset.count(String) to find the count of a word
回答by hthserhs
I hope you realize what the test = e
statement is doing. After this statement executes both test
and e
refer to the sameobject.
我希望你明白这个test = e
声明在做什么。此语句执行后两者test
并e
引用同一个对象。
If anyone of them modifies the list, the other sees it as they both are looking at the same object.
如果他们中的任何一个修改了列表,另一个人就会看到它,因为他们都在看同一个对象。
If this is not intended you need to clonethe list before assigning it to another list reference.
如果这不是您想要的,您需要在将其分配给另一个列表引用之前克隆该列表。
回答by JoshDM
This doesn't affect your out of bounds issue, but you are removing elements from your list while still evaluating it. If you remove an element, you should call i--
afterwards, or you skip the next entity (which is re-indexed) for evaluation.
这不会影响您的越界问题,但您正在从列表中删除元素,同时仍在对其进行评估。如果删除一个元素,则应在i--
之后调用,或者跳过下一个实体(重新索引)进行评估。
Also of note regarding your code, I see you are trying to make a copy of your list, but standard assignment means test
and e
both point to the same instance. You need to use Collections.copy()
see this SO thread on how to use the class.
关于您的代码还要注意的是,我看到您正在尝试复制您的列表,但标准分配意味着test
并且e
两者都指向同一个实例。您需要使用Collections.copy()
查看此 SO 线程以了解如何使用该类。