I was trying to do something like:
ArrayList<String> getMerged ( String host, String port, String filesToCopy ){
ArrayList<String> merged = new ArrayList<String>();
merged.add(host);
merged.add(port);
merged.addAll(filesToCopy.split(",")); //which is invalid
return merged;
}
I want to know if we can add elements of filesToCopy.split(",") with out having the overhead of using a loop.
Also, if the above operation can be done in a string array, say String[] merged (can pass filesToCopy also as String[] if needed), it would be even better coz in the end, I'll be converting this arrayList into an array.
I'm novice in Java programming, so please don't mind if this is a silly question.
filesToCopy
is a list, why would you need to split the strings up within it? UnlessfilesToCopy
is meant to be a single string containing comma delimited file names? Maybe you should look into varargs arguments for methods....split
.filesToCopy
is an ArrayList so what is that supposed to hypothetically accomplish? Show us what you are trying to do using the loop that you don't want to use.filesToCopy
as a String) you can just putArrays.asList(filesToCopy.split(",");
into theaddAll()
method and it should work fine.