Varargs:
Java has included a feature in JDK 5 that simplifies the creation of methods that require a variable number of arguments. This feature is known as varargs, which is an abbreviation for variable-length arguments. A varargs method is one that accepts a variable number of arguments.
Variable-length arguments could be handled in two ways prior to JDK 5. One method employs overloaded methods (one for each), while another places the arguments in an array and then passes this array to the method. Both are potentially error-prone and necessitate more code. The varargs feature is a better, simpler option.
Arraylist:
The elements of the Java ArrayList class are stored in a dynamic array. It’s similar to an array, but there’s no size limit. At any time, we can add or remove elements. As a result, it is far more adaptable than a traditional array. It’s in the java.util package. It is analogous to the Vector in C++.
In Java, duplicate elements can also exist in an ArrayList. It implements the List interface, so we can use all of the List interface’s methods here. Internally, the ArrayList keeps track of the insertion order.
It derives from AbstractList and implements the List interface.
In this article, we’ll look at how to pass an arraylist to a method that takes vararg as an argument.
Passing an ArrayList to Varargs Method
Assume we have a method that accepts Integer varargs and computes their product.
int findProduct(Integer... numbers) { // declaring product as 1 int product = 1; // computing the product for (int ele : numbers) { product = product * ele; } return product; }
We can call this function by many ways some of them are:
1)This function can now be called by passing arguments separately i.e
findProduct(5, 6, 7);
2)passing an array
Integer[] array1 = { 2, 8, 1, 1, 3, 1 }; findProduct(array1);
3)passing Arraylist
Assume we want to pass an ArrayList to the vararg parameter of this method.
ArrayList<Integer> intlist = new ArrayList<>(); findProduct(intlist);
It will result in a compile error because we cannot directly pass an ArrayList to vararg in a method parameter. So, let’s see how we can do this.
To accomplish this, we must first convert our ArrayList to an Array and then pass it to the method that expects vararg. This can be done in a single line, i.e.
findProduct(intlist.toArray(new Integer[0]));
Below is the implementation:
import java.io.*; import java.lang.*; import java.util.*; class Codechef { // function which Accepts a variable number of arguments // and computes the product of all passed elements. int findProduct(Integer... numbers) { // declaring product as 1 int product = 1; // computing the product for (int ele : numbers) { product = product * ele; } return product; } public static void main(String[] args) throws java.lang.Exception { // taking a object of given class Codechef classobject = new Codechef(); // initializing array with some values Integer[] array1 = { 1, 2, 3, 4, 5, 6 }; // converting array to array list ArrayList<Integer> intlist = new ArrayList<>(Arrays.asList(array1)); int product = classobject.findProduct( intlist.toArray(new Integer[0])); // printing the sum System.out.println("Product is = " + product); } }
Output:
Product is = 720
Related Programs:
- python how to find all indexes of an item in a list
- python how to check if an item exists in list
- python how to create an empty set and append items to it
- python how to create an empty list and append items to it
- pandas how to create an empty dataframe and append rows columns to it in python
- python how to add an element in list append vs extend
- python how to insert an element at specific index in list