Skip to Content

Java ArrayList to String Array Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         Java ArrayList to String Array Example
  3.         This Java ArrayList to String Array example shows how to convert ArrayList to String array
  4.         in Java.
  5.  */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9.  
  10. public class ArrayListToStringArrayExample {
  11.        
  12.         public static void main(String args[]){
  13.                
  14.                 //ArrayList containing string objects
  15.                 ArrayList<String> aListDays = new ArrayList<String>();
  16.                 aListDays.add("Sunday");
  17.                 aListDays.add("Monday");
  18.                 aListDays.add("Tuesday");
  19.                
  20.                 /*
  21.                  * To convert ArrayList containing String elements to String array, use
  22.                  * Object[] toArray() method of ArrayList class.
  23.                  *
  24.                  * Please note that toArray method returns Object array, not String array.
  25.                  */
  26.                
  27.                 //First Step: convert ArrayList to an Object array.
  28.                 Object[] objDays = aListDays.toArray();
  29.                
  30.                 //Second Step: convert Object array to String array
  31.                 String[] strDays = Arrays.copyOf(objDays, objDays.length, String[].class);
  32.                
  33.                 System.out.println("ArrayList converted to String array");
  34.                
  35.                 //print elements of String array
  36.                 for(int i=0; i < strDays.length; i++){
  37.                         System.out.println(strDays[i]);
  38.                 }
  39.         }
  40. }
  41.  
  42. /*
  43. Output of above given ArrayList to String Array example would be
  44. ArrayList converted to String array
  45. Sunday
  46. Monday
  47. Tuesday
  48. */