java 在java中连接两个int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4140721/
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
concatenating two int in java
提问by aherlambang
Is there an easy way without having to use java library to concat two int? Maybe math?
有没有一种简单的方法而不必使用java库来连接两个int?也许数学?
say I have 9 and 10 , then I want it to be 910, or 224 and 225 then I want it to be 224225.
假设我有 9 和 10 ,那么我希望它是 910,或者 224 和 225 然后我希望它是 224225。
回答by Jeremy
Anything in java.lang.*
should be fair game...
任何事情都java.lang.*
应该是公平的游戏......
int a = Integer.parseInt(Integer.toString(9) + Integer.toString(10));
Addendum:
附录:
I do not like the following the syntax because the operator overloading doesn't declare intent as clearly as the above. Mainly because if the empty string is mistakenly taken out, the result is different.
我不喜欢下面的语法,因为运算符重载没有像上面那样清楚地声明意图。主要是因为如果误取出空字符串,结果就不一样了。
int a = Integer.parseInt(9 + "" + 10);
回答by Mark Elliot
This will give you an integer back as expected but only works when b > 0
.
这将按预期为您返回一个整数,但仅在b > 0
.
int a = 224;
int b = 225;
int c = (int) Math.pow(10, Math.floor(Math.log10(b))+1)*a + b; // 224225
Just a quick explanation: This determines the number of digits in b
, then computes a multiplication factor for a
such that it would move in base 10 by one more digit than b
.
只是一个简单的解释:这确定了 中的位数b
,然后计算了一个乘法因子,a
以便它在基数 10 中比 多一位b
。
In this example, b
has 3 digits, floor(log10(b))
returns 2 (do this intuitively as 10^2=100
, 10^3 = 1000
, we're somewhere in between at 225). Then we compute a multiplication factor of 10^(2+1)
, this is 1000
. When we multiply a
by 1000 we get 224000
. Adding 224000
to 225
yields the desired 224225
.
在这个例子中,b
有 3 个数字,floor(log10(b))
返回 2(直观地这样做,10^2=100
, 10^3 = 1000
,我们在 225 之间的某个位置)。然后我们计算 的乘法因子10^(2+1)
,这是1000
。当我们乘以a
1000 时,我们得到224000
。添加224000
到225
产生所需的224225
.
This fails at b == 0
because log10(0)
is undefined.
这将失败,b == 0
因为log10(0)
未定义。
回答by aherlambang
You (likely) want string concatenation (you may also want an integer result, if that is the case, see the other answers). If this is true, about the concatenation, imagine a and b are integers:
您(可能)想要字符串连接(您可能还想要一个整数结果,如果是这种情况,请参阅其他答案)。如果这是真的,关于串联,假设 a 和 b 是整数:
"" + a + b
This works because the + operator is overloaded if either operand is a String. It then converts the other operand to a string (if needed) and results in a new concatenated string. You could also invoke Integer.toString(a) + Integer.toString(b)
or using an appropriate String.format
but those methods are more long-winded.
这是有效的,因为如果任一操作数是字符串,则 + 运算符将被重载。然后它将另一个操作数转换为字符串(如果需要)并生成一个新的连接字符串。您也可以调用Integer.toString(a) + Integer.toString(b)
或使用适当String.format
的方法,但这些方法比较冗长。
回答by Westy92
Here is my version which works when a,b >= 0.
这是我的版本,当 a,b >= 0 时有效。
It is a bit longer, but is 10x faster than the log approach and 5x faster than appending strings.
它有点长,但比日志方法快 10 倍,比附加字符串快 5 倍。
int concat(int a, int b)
{
if ( b == 0 )
a *= 10;
else
{
int tempB = b;
while ( tempB > 0 )
{
tempB /= 10;
a *= 10;
}
}
return a + b;
}
Feel free to modify this to work for negative numbers.
随意修改它以适用于负数。
回答by Conor
a + "" + b
a + "" + b
results in error "incompatible types"
导致错误“类型不兼容”
// The left operand to previousOperator.
private int leftOperand;
leftOperand = leftOperand + " " + number;
leftOperand = leftOperand + " " + number;
number is defined as int in the method declaration
number 在方法声明中被定义为 int
This works
这有效
import java.lang.*
导入 java.lang.*
leftOperand = Integer.parseInt(Integer.toString(leftOperand) + Integer.toString(number));
leftOperand = Integer.parseInt(Integer.toString(leftOperand) + Integer.toString(number));