英文:
Cannot infer type arguments for ArraySortedMap<>
问题
我有一个关于泛型的作业。-jdk.13.02,Eclipse。
Map<String, Person> personByFirstName = new ArrayMap<>();
这行代码给我一个错误:"无法为ArrayMap<K,V>推断类型参数"
这是接口Map的头部:
public interface Map<K, V> extends Iterable<Map.Entry<K, V>>
这是ArrayMap类:
public class ArrayMap<K, V> implements Map<K, V>{
@SuppressWarnings("unchecked")
protected Entry<K, V>[] array = (Entry<K, V>[]) new Entry[16];
protected int nEntries = 0;
ArrayMap(){
}
...等等。
根据作业要求,@suppresswarnings是必需的。
有人知道这里可能出了什么问题吗?还是提供的信息不够?我只是猜想问题可能出在类的头部/泛型上。我尝试了谷歌搜索错误和其他事情,但没有找到任何信息。
谢谢
英文:
I have an assignment regarding generics. -jdk.13.02, Eclipse.
Map<String, Person> personByFirstName = new ArrayMap<>();
This line of code gives me the error: "cannot infere type arguments for ArrayMap<K, V>"
this is the header of the Interface Map:
public interface Map<K, V> extends Iterable<Map.Entry<K, V>>
this is the class ArrayMap:
public class ArrayMap <K, V> implements Map<K, V>{
@SuppressWarnings("unchecked")
protected Entry<K, V>[] array = (Entry<K, V>[]) new Entry[16];
protected int nEntries = 0;
ArrayMap(){
}
... etc.
The @suppresswarnings was required as per the assignment.
Does anyone has an idea what could be going on here or is it not enough information? I am just guessing that the problem lies in the class headers/ generics.
I tried googling the error and other things but have not found anything.
Thanks
答案1
得分: 1
我刚刚自己实现了你的代码,它对我起作用。
然而,我刚开始确实看到了那个错误,在我有以下这行代码:
Map<String, Person> personByFirstName = new ArrayMap<>();
这是因为我导入了java.util.Map。确保你导入你自己的Map,而不是java.util.Map。
英文:
I have just implemented your code myself and it works for me.
However, I did see that error at first, where I had this line:
Map<String, Person> personByFirstName = new ArrayMap<>();
It's because I was importing java.util.Map. Make sure you import your own Map, instead of java.util.Map.
答案2
得分: 0
你的代码对我来说没问题:personbyfirst name declaration
这是你拥有的同样的ArrayMap。所以我猜你要么以一种错误的方式实现了一个方法,要么弄乱了一个导入。尝试用相同的代码创建一个新项目。
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class ArrayMap<K, V> implements Map<K, V> {
@SuppressWarnings("unchecked")
protected Entry<K, V>[] array = (Entry<K, V>[]) new Entry[16];
protected int nEntries = 0;
ArrayMap(){
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public V get(Object key) {
return null;
}
@Override
public V put(K key, V value) {
return null;
}
@Override
public V remove(Object key) {
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
}
@Override
public void clear() {
}
@Override
public Set<K> keySet() {
return null;
}
@Override
public Collection<V> values() {
return null;
}
@Override
public Set<Entry<K, V>> entrySet() {
return null;
}
}
英文:
Your code works fine for me:personbyfirst name declaration
Heres the same arraymap you have. So id guess you either implemented a method in a broken way or messed up an import. Try to make a new project with the same code.
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class ArrayMap <K, V> implements Map<K, V> {
@SuppressWarnings("unchecked")
protected Entry<K, V>[] array = (Entry<K, V>[]) new Entry[16];
protected int nEntries = 0;
ArrayMap(){
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public V get(Object key) {
return null;
}
@Override
public V put(K key, V value) {
return null;
}
@Override
public V remove(Object key) {
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
}
@Override
public void clear() {
}
@Override
public Set<K> keySet() {
return null;
}
@Override
public Collection<V> values() {
return null;
}
@Override
public Set<Entry<K, V>> entrySet() {
return null;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论