我将以下Python代码转换为Java代码,如下所示:
import random
howMany = random.randint(0,1000)
stats = {}
for i in range(howMany):
value = random.randint(0,500)
stats.setdefault(value,0)
stats[value]+=1
for item in stats:
if stats[item] > 1:
print item
以下是我迄今为止编写的Java代码:
import java.util.Random;
import java.util.*;
public class PythonToJava
{
public static void main(String[] args)
{
Random rm = new Random();
int i = rm.nextInt(1000);
HashMap<Integer,Integer> stats = new HashMap<Integer,Integer>();
System.out.println("Random Number Generated is: " + i);
for (int j = 0; j<i; j++)
{
int value = rm.nextInt(500);
System.out.println("The value of VALUE is " + value);
DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();
defaultvalue.put(value,0);
}
}
}
public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
{
private final Map<Integer, Integer> map;
private final Integer defaultValue;
public DefaultingMap(Map<Integer, Integer> map, Integer defaultValue)
{
this.map = map;
this.defaultValue = defaultValue;
}
@Override public Integer get(Object key)
{
Integer ret = map.get(key);
if (ret == null)
{
ret = defaultValue;
}
return ret;
}
@Override public int size()
{
return map.size();
}
// etc
}
但在以下行中出现错误:
Java代码:
DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();
错误是:构造函数DefaultingMap()未定义
在public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
Error is : Multiple markers at this line
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.remove(Object)
- The type parameter Integer is hiding the type Integer
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.put(Integer, Integer)
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.keySet()
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.values()
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.containsKey(Object)
- The type parameter Integer is hiding the type Integer
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.containsValue(Object)
- Duplicate type parameter Integer
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.entrySet()
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.putAll(Map<? extends Integer,? extends Integer>)
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.isEmpty()
- The type DefaultingMap<Integer,Integer> must implement the inherited abstract method
Map<Integer,Integer>.clear()
有人能解释为什么吗?你知道吗
基本上,我试图设置一个默认值,就像setdefault在python中一样。如果有人能帮我,我将不胜感激。你知道吗
我修改了
至
还有
变成
如果不确定,您需要实现Map接口中的所有方法
对我有用。你知道吗
我看到的是您在
DefaultingMap
中有参数化的构造函数,并且缺少默认构造函数。请添加默认构造函数否则更改此呼叫
为了这个
更新
很明显,您需要覆盖刚刚覆盖的映射中的所有方法。请克服所有的方法。这样就可以解决您的问题
采用以下方法超越
请参阅Method Summary Section以查看要重写的方法
建议 如果您不想超越MAP中的所有方法(因为这确实是一种痛苦),那么您可以使用
implements MAP
,而不是extends HashMap
。有了它,你只需要超越你想要的方法。与您的情况一样,获取和大小相关问题 更多 >
编程相关推荐