dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 22, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Convert an Array of Strings to a List

The java.util.Arrays class has an asList method to convert the argument array into a list. This will be handy when there are large amounts of information that you need to enumerate.

import java.util.Arrays;
import java.util.List;

public class ConvertArrayToList
{ 
   public static void main(String args[])
   {
      ConvertArrayToList convertArrayToList = new ConvertArrayToList();
      convertArrayToList.proceed();
   }
   
   private void proceed()
   {
      //String array containing the week days
      String daysOfWeek[] =  {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      List listOfWeekDays =  Arrays.asList(daysOfWeek); //method to convert into a list
      System.out.println("Week days: "+listOfWeekDays);
   }
}

/*

Expected output:

[root@mypc]# java ConvertArrayToList
Week days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

*/
M S Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date