Java - method parameters, String followed by List

504 views Asked by At

I have a Java method that accepts a String and an String<List> as method parameters. How do I differentiate the String from not being part of the List?

Method:

void returnValues(String sensor, List<String> attributes)

Call:

nexaConnect.returnValues(Arrays.asList("19455746", "blobJson", "deviceMfg", "eventCode", "sensorClass", "sensorUUID", "timeStamp", "uID"));

The only possible work around I can see is to remove the first String and include the value within the List and then get the first value of the list and use it that way. Is there any way to separate them out so that it's not part of the list?

2

There are 2 answers

2
Elliott Frisch On BEST ANSWER

The attempt to call returnValues does not match the formal parameters. Assuming that's what you're asking, then like this.

nexaConnect.returnValues("TheFirst", Arrays.asList("19455746", "blobJson", 
    "deviceMfg", "eventCode", "sensorClass", 
    "sensorUUID", "timeStamp", "uID"));

or save the references to local variables and it looks like,

String sensor = "TheFirst";
List<String> attributes = Arrays.asList("19455746", "blobJson", 
    "deviceMfg", "eventCode", "sensorClass", 
    "sensorUUID", "timeStamp", "uID");
nexaConnect.returnValues(sensor, attributes);
0
AxelH On

For now, you have a method signature

void methodName(String arg1, List<String> arg2)

that you are call using a signature similar to

void methodName(List<String> arg2)

You need to match the signature when you call a method, so pass a String has first argument then the list.

So instead of calling it like

instance.methodName(anInstanceOfList);

First pass a String value

instance.methodName(aStringValue, anInstanceOfList);