Skip to Content

Java Convert int Array To String 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 Convert int Array To String Example
  3.         This Java Convert int Array To String example shows how to find convert an array of int
  4.         to a String in Java.
  5.  */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class ConvertIntArrayToStringExample {
  10.  
  11.         public static void main(String args[]){
  12.                
  13.                 //int array
  14.                 int[] intNumbers = new int[]{1, 2, 3, 4, 5};
  15.                
  16.                 /*
  17.                  * First approach is to loop through all elements of an int array
  18.                  * and append them to StringBuffer object one by one. At the end,
  19.                  * use toString method to convert it to String.
  20.                  */
  21.                
  22.                 //create new StringBuffer object
  23.                 StringBuffer sbfNumbers = new StringBuffer();
  24.                
  25.                 //define the separator you want in the string. This example uses space.
  26.                 String strSeparator = " ";
  27.                
  28.                 if(intNumbers.length > 0){
  29.                        
  30.                         //we do not want leading space for first element
  31.                         sbfNumbers.append(intNumbers[0]);
  32.                        
  33.                         /*
  34.                          * Loop through the elements of an int array. Please
  35.                          * note that loop starts from 1 not from 0 because we
  36.                          * already appended the first element without leading space.s  
  37.                          */
  38.                         for(int i=1; i < intNumbers.length; i++){
  39.                                 sbfNumbers.append(strSeparator).append(intNumbers[i]);
  40.                         }
  41.                        
  42.                 }
  43.                
  44.                 System.out.println("int array converted to String using for loop");
  45.                
  46.                 //finally convert StringBuffer to String using toString method
  47.                 System.out.println(sbfNumbers.toString());
  48.                
  49.                 /*
  50.                  * Second options is to use Arrays class as given below.
  51.                  * Use Arrays.toString method to convert int array to String.
  52.                  *
  53.                  * However, it will return String like [1, 2, 3, 4, 5]
  54.                  */
  55.                
  56.                 String strNumbers = Arrays.toString(intNumbers);
  57.                
  58.                 System.out.println("String generated from Arrays.toString method: " + strNumbers);
  59.                
  60.                 //you can use replaceAll method to replace brackets and commas
  61.                 strNumbers = strNumbers.replaceAll(", ", strSeparator).replace("[", "").replace("]", "");
  62.                
  63.                 System.out.println("Final String: " + strNumbers);
  64.         }
  65. }
  66.  
  67. /*
  68. Output of above given convert int array to String example would be
  69. int array converted to String using for loop
  70. 1 2 3 4 5
  71. String generated from Arrays.toString method: [1, 2, 3, 4, 5]
  72. Final String: 1 2 3 4 5
  73. */