将省略号作为参数传递给 java 方法

Passing elipses as an argument to java method

我有一个 Java 方法,它接受省略号格式的参数。此方法依次调用 api,它再次接受省略号格式的参数:

public void myMethod(String a, String... listOfParam){

   //Method Signature of anotherAPI is anotherAPI(String input, Object ... listOfParams)

   //I call anotherAPI method as described below.
   String result = anotherAPI(a, listOfParam); 
}

但是,当我将 listOfParams 作为参数传递时,我得到 UnsupportedOperationException

请告诉我在这种情况下正确的做法。

编辑

我正在调用 AmazonDynamoDB Java API 的 (http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?index-all.html)addHashOnlyPrimaryKeys方法:

客户端方法如下:

public static List<Item> getAllItems(String tableName,String primaryKeyName, String ... listOfKeys){
    List<Item> itemList = null;
    System.out.println(listOfKeys.toString());
    TableKeysAndAttributes tkaa = new TableKeysAndAttributes(tableName);
    tkaa.addHashOnlyPrimaryKey(primaryKeyName, listOfKeys);
    BatchGetItemOutcome outcome = dynamoDB.batchGetItem(tkaa);
    itemList = outcome.getTableItems().get(tableName);
    return itemList;
}

我测试此方法为:

List<Item> listOfItems = AmazonDynamoDBUtil.getAllItems("myTable","data","1","2","3","4");

这里是字符串类型的数据。

但是,我得到的异常是:

Exception in thread "main" java.lang.UnsupportedOperationException: value type: class [Ljava.lang.String;
        at com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.toAttributeValue(InternalUtils.java:221)
        at com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.toAttributeValueMap(InternalUtils.java:535)
        at com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.toAttributeValueMap(InternalUtils.java:547)
        at com.amazonaws.services.dynamodbv2.document.internal.BatchGetItemImpl.doBatchGetItem(BatchGetItemImpl.java:84)
        at com.amazonaws.services.dynamodbv2.document.internal.BatchGetItemImpl.batchGetItem(BatchGetItemImpl.java:58)
        at com.amazonaws.services.dynamodbv2.document.DynamoDB.batchGetItem(DynamoDB.java:154)
        at awsdynamodb.AmazonDynamoDBUtil.getAllItems(AmazonDynamoDBUtil.java:63)

请告诉我这种方法有什么问题。

API 不支持该方法调用(因此出现 unsupported 异常)。与省略号无关。不要调用那个方法,它不会起作用。

这是有效的:

public class Main {

    public static void main(String[] args) {
        myMethod("test 1");
        myMethod("test 2", "a");
        myMethod("test 3", "a", "b", "c");
    }

    public static void myMethod(String a, String... listOfParam) {
        anotherAPI(a, listOfParam);
    }

    private static void anotherAPI(String a, String... listOfParam) {
        StringBuilder sb = new StringBuilder();
        for (String p : listOfParam) {
            sb.append(p);
            sb.append(", ");
        }
        System.out.println(a + ": " + sb.toString());
    }
}

产生预期结果:

test 1: 
test 2: a, 
test 3: a, b, c, 

从 Java 的角度来看 listOfParam 因为 String... 是一个 String[] 数组。

尽管有警告(您可以通过将对 anotherAPI() 的调用的第二个参数转换为 Object[] 来摆脱警告),以下代码对我来说运行良好:

public class Test
{
  public static String anotherAPI(final String a, final Object... listOfParam)
  {
    for (final Object param : listOfParam)
      System.out.println(param);

    return "Test";
  }

  public static void main(final String[] args)
  {
    myMethod("a", "This", "is", "a", "test");
  }

  public static void myMethod(final String a, final String... listOfParam)
  {
    final String result = anotherAPI(a, listOfParam);
    System.out.println(result);
  }
}

输出:

This
is
a
test
Test

我认为 anotherAPI() 的实现不允许您尝试执行的操作。

对不起各位,我好像打电话给 addHashOnlyPrimaryKey 而不是 addHashOnlyPrimaryKeys :)