Java 两个整数的 Mod 除法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16225883/
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
Mod division of two integers
提问by user2268305
I keep getting the error "The operator % is undefined for the argument type(s) Integer, Integer" I am not quite sure why this is happening. I thought that since modular division cannot return decimals that having integer values would be alright.
我不断收到错误“运算符 % 未定义参数类型整数,整数”我不太确定为什么会发生这种情况。我认为由于模块化除法不能返回小数,因此具有整数值就可以了。
This is happening within a method in a program I am creating. The code is as follows:
这是在我正在创建的程序中的一个方法中发生的。代码如下:
public void addToTable(Integer key, String value)
{
Entry<Integer, String> node = new Entry<Integer, String>(key, value);
if(table[key % tableSize] == null)
table[key % tableSize] = node;
}
The method is unfinished but the error occurs at
该方法未完成,但错误发生在
if(table[key % tableSize] == null)
and
和
table[key % tableSize] = node;
any help or suggestions would be appreciated.
任何帮助或建议将不胜感激。
采纳答案by rgettman
I could get some sample Integer % Integer
code to compile successfully in Java 1.5 and 1.6, but not in 1.4.
我可以获得一些示例Integer % Integer
代码,可以在 Java 1.5 和 1.6 中成功编译,但不能在 1.4 中成功编译。
public static void main(String[] args)
{
Integer x = 10;
Integer y = 3;
System.out.println(x % y);
}
This is the error in 1.4:
这是 1.4 中的错误:
ModTest.java:7: operator % cannot be applied to java.lang.Integer,java.lang.Integer
System.out.println(x % y);
^
The most reasonable explanation is that because Java introduced autoboxing and autounboxing in 1.5, you must be using a Java compiler from before 1.5, say, 1.4.
最合理的解释是,因为Java 在 1.5 中引入了自动装箱和自动拆箱,所以您必须使用 1.5 之前的 Java 编译器,例如 1.4。
Solutions:
解决方案:
- Upgrade to Java 1.5/1.6/1.7.
- If you must use 1.4, use
Integer.intValue()
to extract theint
values, on which you can use the%
operator.
- 升级到 Java 1.5/1.6/1.7。
- 如果必须使用 1.4,请使用
Integer.intValue()
用于提取int
值,您可以在其上使用%
运算符。
回答by Chris Chambers
Try converting the Integers to ints, then run %
.
尝试将整数转换为整数,然后运行%
.
if(table[key.intValue() % tableSize.intValue()] == null)
table[key.intValue() % tableSize.intValue()] = node;
回答by durron597
This works fine for me.
这对我来说很好用。
Integer x = Integer.valueOf(10);
Integer y = Integer.valueOf(3);
int z = x % y;
System.out.println(z);
No problems. Output:
没问题。输出:
1
What error are you getting? What version of Java are you using? It seems that you're using Java below 1.5.
你遇到了什么错误?您使用的是什么版本的 Java?看来您使用的是低于 1.5 的 Java。
回答by paxdiablo
What you're attempting here is called unboxing, the auto-conversion of an object to a primitive type (going the other way is autoboxing).
您在这里尝试的方法称为拆箱,将对象自动转换为原始类型(另一种方式是自动装箱)。
The Java docs have this to say:
Java 文档有这样的说法:
The Java compiler applies unboxing when an object of a wrapper class is:
当包装类的对象是:
- Passed as a parameter to a method that expects a value of the corresponding primitive type.
- Assigned to a variable of the corresponding primitive type.
- 作为参数传递给需要相应原始类型值的方法。
- 分配给相应原始类型的变量。
So one possibility is that you're notdoing one of those things and, although it appears at first glance that you're neither passing your mod expression to a method nor assigning it to a variable, it's valid, at least in Java 6:
因此,一种可能性是您没有做这些事情之一,尽管乍一看您既没有将 mod 表达式传递给方法,也没有将其分配给变量,但它是有效的,至少在 Java 6 中是这样:
class Test {
public static void main(String args[]) {
Integer x = 17;
Integer y = 5;
System.out.println (x % y);
String [] z = new String[10];
z[x % y] = "hello";
}
}
The other possibility is that you're using a pre Java 5 environment, where autoboxing and unboxing was introduced.
另一种可能性是您使用的是 Java 5 之前的环境,其中引入了自动装箱和拆箱。
Best bet in that case is probably to be explicit and use Integer.intValue()
to get at the underlying int
.
在这种情况下,最好的选择可能是明确的并用于Integer.intValue()
获取底层int
.
However you may also want to consider using an int
(not Integer
) for the key and only boxing it up at the point where you needto (when you add it to an Entry
). It may well be faster to use the primitive type, though you should of course benchmark it to be sure.
但是,您可能还想考虑对键使用int
(not Integer
),并且仅在需要的地方将其装箱(当您将其添加到 时Entry
)。使用原始类型可能会更快,但您当然应该对其进行基准测试以确保。