A method declared as

void X(Object... values){};

is similar to a method declared as

void X(Object[] values){};

and can be called with an Object[], though only the first can be called with a variable number of arguments. You can convert your list to an array of using List.toArray(), so that you can call X(list.toArray()). Here's example code demonstrating:

import java.util.Arrays;

public class VarargsExample {

    public static void foo( Object... args ) { 
        System.out.println( "foo: "+Arrays.toString( args ));
    }

    public static void bar( Object[] args ) {
        System.out.println( "bar: "+Arrays.toString( args ));
    }

    public static void main(String[] args) {
        // only foo can be called with variable arity arguments
        foo( 1, 2, 3 );
        // bar( 4, 5, 6 ); // won't compile 

        // both methods can be called with object arrays
        foo( new Object[] { 7, 8, 9 } );
        bar( new Object[] { 10, 11, 12 } );

        // so both can be called with List#toArray results
        foo( Arrays.asList( 13, 14, 15 ).toArray() );
        bar( Arrays.asList( 16, 17, 18 ).toArray() );
    }
}
foo: [1, 2, 3]
foo: [7, 8, 9]
bar: [10, 11, 12]
foo: [13, 14, 15]
bar: [16, 17, 18]
Answer from Joshua Taylor on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to pass an array of elements as separate arguments to a method
r/javahelp on Reddit: How to pass an array of elements as separate arguments to a method
May 31, 2017 -

So I have a method as follows....

public void display(String ...names) {
    ArrayList<String> allNames = new ArrayList<String>();
    for (String name : names) {
        allNames.add(name);
    }
}

I then have a String[] array String[] names of names collected from a command line. I want to be able to pass these strings all at once into the method, but when I try this:

display(names);

I am told this is not allowed. Is there anyway I can pass all of these Strings into the method at once without knowing the size of the String[] array?

๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 08 โ€บ passing-a-list-to-a-varargs-method
Passing a List to a Varargs method
September 11, 2022 - import java.util.List; public class JavaExample { public void displayParam(Integer... args) { System.out.println("Print Varargs method arguments: "); for (int num : args) { System.out.println(num); } } public static void main(String[] args) { JavaExample obj = new JavaExample(); List<Integer> myList = List.of(1, 3, 5, 7, 9); obj.displayParam(myList.toArray(new Integer[0])); } } ... The logic is similar and pretty straight forward. We are converting the ArrayList to array and passing it to the method same as we done in above examples.
Discussions

How to convert a List to variable argument parameter java - Stack Overflow
I have a method which takes a variable length string (String...) as parameter. I have a List with me. How can I pass this to the method as argument? More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Passing List<String> to String... parameter - Stack Overflow
I'm struggling to pass a List of Strings into a method requiring the parameter "String...". ... You'll have to convert the List to a String array in order to use it in the 'varargs' parameter of dummyMethod. You can use toArray with an extra array as parameter. More on stackoverflow.com
๐ŸŒ stackoverflow.com
passing multiple arguments in java - Stack Overflow
I need to receive owner and consumerName as arguments/input for my program. import java.sql.*; class MyClass { public static void main (String[] Owner, String[] consumerName ) throws Exception { Class.forName ("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@//host:port/SID", "username", "password... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - ArrayList with multiple arguments (User input) - Stack Overflow
I am trying to create a App base on user input with ArrayList. But there is some problem that I need to understand first. PS: I want to try with normal value first before going into user input. An... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Programming.Guide
programming.guide โ€บ java โ€บ passing-list-to-vararg-method.html
Java: Passing a list as argument to a vararg method | Programming.Guide
import java.util.List; class Demo { public static void main(String[] args) { List<String> yourList = List.of("hello", "world"); yourVarargMethod(yourList.toArray(new String[0])); } static void yourVarargMethod(String...
Find elsewhere
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 749149 โ€บ java โ€บ Variable-method-argument-list
Variable method argument list (Java in General forum at Coderanch)
January 21, 2022 - Hahaha sorry for pulling your leg. It's Saturday and I'm feeling in a silly mood. While "spread syntax" really is a thing, it's not in Java. Anyway, passing an array to a method that takes varargs will work as hoped, as long as the array element type is a subtype of the varargs type.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_methods_param.asp
Java Method Parameters
Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 628521 โ€บ java โ€บ add-multiple-parameters-ArrayList
How do I add multiple parameters to my ArrayList? (Beginning Java forum at Coderanch)
August 2, 2016 - For example, if I have a class called Teacher with the constructor: public Teacher(double yearsTeaching, boolean isMale, double age) { this.yearsTeaching = yearsTeaching; this.isMale = isMale; this.age = age; } How would I add it to these parameters to my ArrayList in my Tester class? List<Teacher> teachers = new ArrayList<Teacher>(); Thanks!
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ pass-arraylist-as-function-argument
Java Program to Pass ArrayList as the function argument
Here, we have passed languages as the function parameter. import java.util.ArrayList; class Main { public static void percentage(Integer[] marks) { int totalMarks = 300; int obtainedMarks = 0; for(int mark : marks) { obtainedMarks += mark; } // compute average double percent = (obtainedMarks * 100) / totalMarks; System.out.println("Percentage: " + percent); } public static void main(String[] args) { // create an arraylist ArrayList<Integer> marks = new ArrayList<>(); marks.add(67); marks.add(87); marks.add(56); System.out.println("Marks: " + marks); // passing arraylist as function parameter percentage(marks.toArray(new Integer[marks.size()])); } }