我有一个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");
这里的数据是String类型。
然而,我得到的例外是:
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)
请让我知道这种做法有什么不妥。
答案 0 :(得分:1)
这是有效的:
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[]
数组。
答案 1 :(得分:0)
API不支持该方法调用(因此unsupported
异常)。与省略号无关。不要打电话给那种方法,它不会起作用。
答案 2 :(得分:0)
以下代码对我来说运行正常,虽然有一个警告(您可以通过将调用的另一个参数转换为另一个API()到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
我认为另一个API()的实现不允许您尝试做什么。
答案 3 :(得分:0)
对不起伙计们,我似乎是在呼叫addHashOnlyPrimaryKey
而不是addHashOnlyPrimaryKeys
:)