InterviewSolution
Saved Bookmarks
| 1. |
Given an array in Java, convert it to a collection. |
|
Answer» We can convert an array to a COLLECTION using the asList() METHOD of the Arrays CLASS in Java. //including the required header filesimport java.util.*;public class Convert_Array_To_Collection { public static void main(String args[]) { //creating a sample array String sample_array[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; int length_array = sample_array.length; System.out.println("The INPUT elements are as follows : "); for(int i = 0; i < length_array; i ++) { System.out.print(sample_array[i] + " "); } System.out.println();// setting the print cursor to the next line List converted_list = Arrays.asList(sample_array);// converting the array to a list // print CONVERTED elements System.out.println("The converted list is as follows : " + converted_list); }}Output: The input elements are as follows : Monday Tuesday Wednesday Thursday Friday Saturday Sunday The converted list is as follows : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] |
|