Reputation: 11
I am trying to combine two arrays in Java, one with strings and another with integers:
int [] intArray = {1, 2, 3};
String [] strArray = {"Hello", "World"};
I am trying to get two results like following:
Object [] combinedObjects = {1, 2, 3, "Hello", "World"};
String [] combinedStrings = {"1", "2", "3", "Hello", "World"};
Upvotes: 0
Views: 922
Reputation: 59960
You should to convert the Integer
values to String
to solve your problem, because the Array can have one type of information :
public static void main(String args[]) {
int[] intArray = {1, 2, 3};
String[] strArray = {"Hello", "World"};
String[] combinedStrings = new String[intArray.length + strArray.length];
int i = 0;
while (i < intArray.length) {
//convert int to string with adding an empty string
combinedStrings[i] = intArray[i] + "";
i++;
}
int j = 0;
while (j < strArray.length) {
combinedStrings[i] = strArray[j];
i++;
j++;
}
for (String val : combinedStrings) {
System.out.println(val);
}
}
You can learn more about arrays in Oracle tutorial, part Creating, Initializing, and Accessing an Array
Upvotes: 0
Reputation: 4588
If you are not stucked to very old java version there is seldom a good reason to use Array. Especially if you want to operate on the array, enlarge or reduce it. The java collection framework is far flexibler. Since java8 the introduction of streams on collections offers a wide range of operations in a very compact coding.
Using streams I would solve your problem as following:
Object [] combinedObjects = Stream.concat(
Arrays.stream( intArray).boxed(),
Arrays.stream( strArray))
.toArray(Object[]::new);
String [] combinedStrings = Stream.concat(
Arrays.stream( intArray).mapToObj( i -> "" + i),
Arrays.stream( strArray))
.toArray(String[]::new);
If your input and your desired output should be a Collection then the code would appear even a little shorter:
Collection<Object> combined = Stream.concat(
intCollection.stream(),
strCollection.stream())
.collect( Collectors.toList() );
Upvotes: 0
Reputation: 143
Edit: your question was changed after I posted my answer and it seems a more fitting answer has been posted, Instead of deleting my post i'm going to leave the last bit here in case you need to do any conversion from your joined array later in your project.
You also have the option to parse your data (this may be useful to you if you ever want to get the int's back from your array.
int tempInt = Integer.parseInt(tempString);
or alternatively:
String tempString = String.valueOf(tempArray[i]);
A good reference for changing types can be found at javadevnotes
Upvotes: 1
Reputation: 3391
you have two approachs :
1 - using arrayList:
ArrayList a = new ArrayList();
for(int i = 0 ; i < intArray.length ; i++)
a.add(intArray[i]);
for(int i = 0 ; i < strArray.length ; i++)
a.add(strArray[i]);
now you have answer in ArrayList
2 - use String.valueof() method :
String combinedStrings[] = new String[strArray.length+intArray.length];
int index= 0;
for(int i = 0 ; i < strArray.length ; i++)
combinedStrings[index++] = strArray[i];
for(int i = 0 ; i < intArray.length ; i++)
combinedStrings[index++] = String.valueOf(intArray[i]);
now you have answer in combinedStrings array
Upvotes: 0