Concatenate multiple arrays into String in Java
In this tutorial, you will learn how to concatenate multiple arrays into a String in Java.
So, concatenation simply means to merge. You have to merge the arrays into a String, an array can be of any data type but the final result should be a String. Initially, you need to concatenate all the given arrays into a single array of the same data type, you can achieve this using type casting into a common data type.
When all the given arrays are merged into a single array then you need to convert it into a string. For this, you can take a string variable and then convert all the elements into a string and eventually, you need to add all the elements in the array to the string variable. The resultant will be a String of all the merged arrays which is our desired output.
Concept to merge the array
Merge or concatenate means to add multiple arrays into a single array. For example, you have two arrays one is of integer data type and the other is of character data type. Our resultant array should be of String array containing elements of both arrays.
Input: 1st Array: 2 4 5 8 9 6 7 1 3 5 2nd Array: C O D E S P E E D Y Resultant Array: 2 4 5 8 9 6 7 1 3 5 C O D E S P E E D Y
So, your initial step would be to input multiple arrays. After all the arrays are defined next step would be to initialize a common array to merge all the arrays into a single data type. In this problem we have to convert arrays into string, so we can take string string as our common array. Next step is to convert all the elements of the array into the string. You can convert the elements of any data type to string using String.valueof()
function. Further you need to initialize a string variable and store the values of the string array to the string variable.
Boolean Array: true false false true Integer Array: 4 5 6 8 Using function --> String.valueof() to convert the elements to string String Array: 4 5 6 8 true false false true
After combining the resultant string array into a string variable with proper spacing then display the string that would be our output.
Steps required:
- Initialize and declare the array elements.
- Initialize the String array to store the converted elements.
- Convert the elements of the array into the string by using String.valueOf() function.
- Put the string value into the string array.
- Repeat the above two steps for all the arrays left.
- Store the string array elements into a string variable.
- Display the string variable.
Java program to concatenate multiple arrays into a single string
import java.io.*; import java.util.*; class Main { public static void main(String[] args) { boolean[] booleanarray= new boolean[] { true, true, false, true }; char[] characterarray= new char[] { 's', 'p', 'e', 'e', 'd','y' }; double[] doublearray= new double[] { 1, 2, 3, 4 }; int[] integerarray= new int[] { 1, 2, 3, 4 }; String s=""; String[] sr= new String[booleanarray.length+characterarray.length+integerarray.length+doublearray.length]; int x=0; for(int i=0;i<booleanarray.length;i++) { sr[x++]=String.valueOf(booleanarray[i]); } for(int i=0;i<characterarray.length;i++) { sr[x++]=String.valueOf(characterarray[i]); } for(int i=0;i<doublearray.length;i++) { sr[x++]=String.valueOf(doublearray[i]); } for(int i=0;i<integerarray.length;i++) { sr[x++]=String.valueOf(integerarray[i]); } for(int i=0;i<sr.length;i++) { s=s+sr[i]; s=s+" "; } System.out.println("String of the concatenated array is: \n"+s); } }
The final result of the solution:
OUTPUT: String of the concatenated array is: true true false true s p e e d y 1.0 2.0 3.0 4.0 1 2 3 4
Leave a Reply