无法推断ArraySortedMap<>的类型参数。

huangapple go评论78阅读模式
英文:

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&lt;String, Person&gt; personByFirstName = new ArrayMap&lt;&gt;();

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&lt;K, V&gt; extends Iterable&lt;Map.Entry&lt;K, V&gt;&gt;

this is the class ArrayMap:

public class ArrayMap &lt;K, V&gt; implements Map&lt;K, V&gt;{
	@SuppressWarnings(&quot;unchecked&quot;)
	protected Entry&lt;K, V&gt;[] array = (Entry&lt;K, V&gt;[]) 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&lt;String, Person&gt; personByFirstName = new ArrayMap&lt;&gt;();

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 &lt;K, V&gt; implements Map&lt;K, V&gt; {
@SuppressWarnings(&quot;unchecked&quot;)
protected Entry&lt;K, V&gt;[] array = (Entry&lt;K, V&gt;[]) 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&lt;? extends K, ? extends V&gt; m) {
}
@Override
public void clear() {
}
@Override
public Set&lt;K&gt; keySet() {
return null;
}
@Override
public Collection&lt;V&gt; values() {
return null;
}
@Override
public Set&lt;Entry&lt;K, V&gt;&gt; entrySet() {
return null;
}
}

huangapple
  • 本文由 发表于 2020年4月3日 19:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定