Is it possible to send a variable number of arguments to a JavaScript function?

Is it possible to send a variable number of arguments to a JavaScript function, from an array? var arr = [‘a’,’b’,’c’] var func = function() { // debug alert(arguments.length); // for(arg in arguments) alert(arg); } func(‘a’,’b’,’c’,’d’); // prints 4 which is what I want, then ‘a’,’b’,’c’,’d’ func(arr); // prints 1, then ‘Array’ I’ve recently written … Read more

How to pass an ArrayList to a varargs method parameter?

Basically I have an ArrayList of locations: ArrayList<WorldLocation> locations = new ArrayList<WorldLocation>(); below this I call the following method: .getMap(); the parameters in the getMap() method are: getMap(WorldLocation… locations) The problem I’m having is I’m not sure how to pass in the WHOLE list of locations into that method. I’ve tried .getMap(locations.toArray()) but getMap doesn’t … Read more

Can I pass an array as arguments to a method with variable arguments in Java?

I’d like to be able to create a function like: class A { private String extraVar; public String myFormat(String format, Object … args){ return String.format(format, extraVar, args); } } The problem here is that args is treated as Object[] in the method myFormat, and thus is a single argument to String.format, while I’d like every … Read more

How can I convert the “arguments” object to an array in JavaScript?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. The arguments object in JavaScript is an odd wart—it acts just like an array in most situations, but it’s not actually an array object. … Read more