I am trying to join int[]
(array of int) using Google Guava's Joiner class.
Example:
int[] array = { 1, 2, 3 }; String s = Joiner.on(", ").join(array); // not allowed
I checked StackOverflow and Google. There is no "one-liner" in foundation classes to convert int[]
to Integer[]
or List<Integer>
. It always requires a for loop, or your own hand-rolled helper function.
Any advice?
To join elements of given string array strArray with a delimiter string delimiter , use String. join() method. Call String. join() method and pass the delimiter string delimiter followed by the string array strArray .
Example 1: Concatenate Two Arrays using arraycopy Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; String result = StringUtils. join(ArrayUtils. toObject(arr), " - ");
Ints is a Guava library containing helper functions.
Given int[] array = { 1, 2, 3 }
you can use the following:
String s = Joiner.on(", ").join(Ints.asList(array));
Or more succinctly:
String s = Ints.join(", ", array);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With