In this Java Tutorial, we learned how to use Arrays.asList() method of java.util package, to create a List from objects or an array, with the help of example programs. asList() method returns a List of type backed by the type of elements, which is Car in this example. Return "(" + this.name + "," + this.units + ")" Ĭall Arrays.asList() method and pass the objects as arguments. You can also provide elements of user defined class type to asList() method as arguments. Output Example 3 – Arrays.asList() with Class Objects asList() method returns a List of type backed by the type of elements that we passed to it. List list = Arrays.asList("apple", "banana", "cherry") Ĭall Arrays.asList() method and pass the elements as arguments. The elements has to be separated by comma. You can also provide multiple elements to asList() method as arguments. Since, we have passed a string array, asList() method returns a List.ĪDVERTISEMENT Example 2 – Arrays.asList() with Elements as Argument Call Arrays.asList() method with arr passed as argument to it. Initialize string array arr with three strings. In this example, we will take a string array, and convert it to List using asList() method. Example 1 – Arrays.asList() with Array as argument If there is any discrepancy with the type of elements, the method can cause a compilation error. The type of items in the List is same as that of the elements passed as argument to the method. The declaration of asList() method in class is public static List asList(T. At the end of this article, you will understand the following pointers in Java which are related to Java Arrays. Please read our previous article, where we discussed Strings in Java with Examples.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |