Java varArgs示例,用于计算数字之和(调用与输入参数数量无关的相同方法)
可能重复: 是否有一种方法可以从其他函数调用泛型函数,而不考虑输入参数? 如果我的输入数据类型都相同,则为int-only或String-onlyJava varArgs示例,用于计算数字之和(调用与输入参数数量无关的相同方法),java,Java,可能重复: 是否有一种方法可以从其他函数调用泛型函数,而不考虑输入参数? 如果我的输入数据类型都相同,则为int-only或String-only public class sumOfNo { public static void main(String[] arg) { /** * Normal way of calling function to calculate sum. */ int result = sum(1, 2); int
public class sumOfNo {
public static void main(String[] arg) {
/**
* Normal way of calling function to calculate sum.
*/
int result = sum(1, 2);
int result1 = sum(1, 2, 3);
}
试着像这样来
public int sum(int... args) {
int sum = 0;
for (int i : args)
sum += i;
return sum;
}
然后你可以打电话
sum(3);
sum(3, 4, 5);
sum(3, 4, 5, 6, 7);
erit:amit快了5秒:试试这个:
public class Main
{
public static void main(String[] argv)
{
int result1 = sum(1, 2);
int result2 = sum(1, 2, 3);
int result3 = sum(1, 2, 4, 5, 6, 1000);
System.out.println(result1 + "\n" + result2 + "\n" + result3);
}
private static int sum(int... args)
{
int ret = 0;
if (args != null)
{
for (int val : args)
{
ret += val;
}
}
return ret;
}
}
快速接近
这可能是一个解决方案注意:始终使用有意义的变量名!:
private static int sum(int i, int... others) {
int sum = i;
if(others!=null)
for(int other : others) {
sum+=other;
}
}
System.out.println("sum:" + sum);
return sum;
}
注意这些论点。由于求1个数的和不是很有意义,所以此构造确保至少有1个整数进入。这个函数还检查vararg中的null值
充满感情。。。
执行此操作时,快速方法会发生什么情况:
int veryBigNumber = sum(Integer.MAX_VALUE, 1);
事实上,veryBigNumber将是==Integer.MIN\u值
这可能是个问题。由于Java在溢出发生时不会抛出异常,因此可能会导致错误的结果。您可以检查溢出:
private static int aLittleBitSaferSum(int i, int... others) throws ArithmeticException {
int sum = i;
if(others!=null)
for(int other : others) {
if(Integer.MAX_VALUE-other<sum) {
throw new ArithmeticException("Sum would be too large to fit in int");
}
if(Integer.MIN_VALUE+other>sum) {
throw new ArithmeticException("Sum would be too small to fit in int");
}
sum+=other;
}
}
System.out.println("sum: " + sum);
return sum;
}
这将导致Integer.MAX_值-它将不进行检查
拓展视野
不要害怕!上述问题也可以解决。例如,通过提供一个聪明的算法对操作数进行排序,使部分结果始终适合int范围,但我认为这是一个需要解决的问题。。。而且会花费大量的计算能力
但是,通过扩展函数正在处理的值的范围,它可以做得更好:
private static int aLittleBitSaferSum(int i, int... others) throws ArithmeticException {
long sum = i;
if(others!=null)
for(int other : others) {
if(Long.MAX_VALUE-other<sum) {
throw new ArithmeticException("Sum would be too large for this algorithm to deal with");
}
if(Long.MIN_VALUE+other>sum) {
throw new ArithmeticException("Sum would be too small for this algorithm to deal with");
}
sum+=other;
}
}
if(Integer.MAX_VALUE<sum) {
throw new ArithmeticException("Sum would be too large to fit in int");
}
if(Integer.MIN_VALUE>sum) {
throw new ArithmeticException("Sum would be too small to fit in int");
}
System.out.println("sum: " + sum);
return (int)sum;
}
由于所有BigInteger的特性,这会更慢,但是上面的函数没有问题。因此,与其他选项一样,它现在会少一点,但额外的内容是有代价的。我没有回答,只是编辑并整理了OPou提供的答案,对,我没有看到。这意味着这是我喝早茶的最佳时间。你有什么问题吗?问题是如何使用泛型实现它?如果只是关于varargs,为什么你会问,并在几秒钟内给出答案?jlordo:实际上很多人不知道如何使用varargs。我在网上搜索了这个,但找不到答案,所以当我找到解决方案时,我把它贴了出来,认为它可能对其他人有帮助@艾米特:是的,这就是我想要分享的。感谢您提供参考。请注意,该代码不是空安全的。看看@restricteur提供的代码,应该这样做
private static int aLittleBitSaferSum(int i, int... others) throws ArithmeticException {
int sum = i;
if(others!=null)
for(int other : others) {
if(Integer.MAX_VALUE-other<sum) {
throw new ArithmeticException("Sum would be too large to fit in int");
}
if(Integer.MIN_VALUE+other>sum) {
throw new ArithmeticException("Sum would be too small to fit in int");
}
sum+=other;
}
}
System.out.println("sum: " + sum);
return sum;
}
sum(Integer.MAX_VALUE, 1, -1);
private static int aLittleBitSaferSum(int i, int... others) throws ArithmeticException {
long sum = i;
if(others!=null)
for(int other : others) {
if(Long.MAX_VALUE-other<sum) {
throw new ArithmeticException("Sum would be too large for this algorithm to deal with");
}
if(Long.MIN_VALUE+other>sum) {
throw new ArithmeticException("Sum would be too small for this algorithm to deal with");
}
sum+=other;
}
}
if(Integer.MAX_VALUE<sum) {
throw new ArithmeticException("Sum would be too large to fit in int");
}
if(Integer.MIN_VALUE>sum) {
throw new ArithmeticException("Sum would be too small to fit in int");
}
System.out.println("sum: " + sum);
return (int)sum;
}
private static int aSafeButSlowerSum(int i, int... others) throws ArithmeticException {
BigInteger sum = BigInteger.valueOf(i);
BigInteger intMax = BigInteger.valueOf(Integer.MAX_VALUE); //should be a private static final class variable
BigInteger intMin = BigInteger.valueOf(Integer.MIN_VALUE); //should be a private static final class variable
if(others!=null)
for(int other : others) {
sum=sum.add(BigInteger.valueOf(i));
}
}
if(intMax.compareTo(sum)<0) {
throw new ArithmeticException("Sum would be too large to fit in int");
}
if(intMin.compareTo(sum)>0) {
throw new ArithmeticException("Sum would be too small to fit in int");
}
System.out.println("sum: " + sum.toString());
return sum.intValue;
}