简体   繁体   English

如何在varargs中测试arg

[英]How to test arg in varargs

I have a method that accepts varargs: 我有一个接受varargs的方法:

public static void main(String... args) throws Exception {

I'm trying to set the value of a variable based upon if an argument is passed or not. 我试图根据是否传递参数来设置变量的值。 In this case, I want to know if an 8th argument has been passed and if so set the variable's value to that argument's value, otherwise use an empty string. 在这种情况下,我想知道是否已经传递了第8个参数,如果是,则将变量的值设置为该参数的值,否则使用空字符串。 I cannot hard code in args[8] on the boolean true result portion because that results in a compile error when it's ran with only 7 arguments. 我不能在布尔值真实结果部分的args [8]中进行硬编码,因为当它只用7个参数运行时会导致编译错误。 So I tried the following but it also errors out: 所以我尝试了以下但它也错了:

String titlePrefix = ((args.length!=8) ?  "" : args[args.length]);

What is the correct way to test if an argument has been passed in this case and use it's value only if it has been passed? 在这种情况下测试参数是否正确的正确方法是什么,只有在传递了它之后才使用它的值? I know that the above would give me problems should this method ever exceed 8 arguments, but for now, let's just assume that it will never have more than 8. 我知道如果这种方法超过8个参数,上面的内容会给我带来问题,但是现在,让我们假设它永远不会超过8个。

Thank you in advance. 先感谢您。

args [args.length]应为args [args.length -1]

String titlePrefix = ((args == null || args.length<8) ?  "" : args[7]);

And why? 为什么?

First, you may need the 9th or the 10th argument in the future. 首先,您将来可能需要第9或第10个参数。 Having 9 arguments means that the 8th argument is present, so you should test whether you have at least 8 arguments, not whether you have exactly 8 arguments. 有9个参数意味着第八参数存在,所以你应该测试你是否有至少 8个参数,你不是是否有完全相同的参数8。

Second, for the same reason, you should not be assigning the last argument, but the exact 8th argument. 其次,出于同样的原因,你不应该分配最后一个参数,而是确切的第8个参数。 After all, the last argument could be the 9th or 10th or 100th. 毕竟,最后一个论点可能是第9或第10或第100。

But even if you were testing the last argument, please remember that arrays in Java (and varargs is merely a disguised array) start from index 0 to index length-1 . 但即使您正在测试最后一个参数,请记住Java中的数组(和varargs只是一个伪装的数组)从索引0开始到索引length-1 The first argument is args[0] , the second is args[1] and the eighth is args[7] . 第一个参数是args[0] ,第二个args[1]args[1] ,第八个args[7]args[7] So to test the last argument, you have to use args[args.length-1] . 因此,要测试最后一个参数,必须使用args[args.length-1]

When dealing with varargs, it's usually best to also test for null first (although when we are talking about the main method, it's probably not going to happen). 在处理varargs时,通常最好先测试null(尽管当我们讨论main方法时,它可能不会发生)。

Check this. 检查一下。 I think that's exactly what you're looking for. 我认为这正是你要找的。

public class Test001 {

    public static void main(String... args) throws Exception {
        for (int i=0; i<8; i++){
            String val = args.length <= i ? "" : args[i];
            System.out.println("arg[" + i + "] = [" + val + "].");
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM