Here's what I came up with. There are several ways to do this and they depend on the tools you are using.
Using StringUtils and ArrayUtils from Commons Lang:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");
You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).
Works at any Java version, from 1.2.
Using Java 8 streams:
Something like this:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" - "));
In fact, there are lot of variations to achieve it using streams.
Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].
Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);
If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:
Use with AIpublic static String myJoin(int[] arr, String separator) {
if (null == arr || 0 == arr.length) return "";
StringBuilder sb = new StringBuilder(256);
sb.append(arr[0]);
//if (arr.length == 1) return sb.toString();
for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);
return sb.toString();
}
Then you can do:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
Answer from spirit on Stack OverflowHere's what I came up with. There are several ways to do this and they depend on the tools you are using.
Using StringUtils and ArrayUtils from Commons Lang:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");
You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).
Works at any Java version, from 1.2.
Using Java 8 streams:
Something like this:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" - "));
In fact, there are lot of variations to achieve it using streams.
Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].
Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);
If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:
Use with AIpublic static String myJoin(int[] arr, String separator) {
if (null == arr || 0 == arr.length) return "";
StringBuilder sb = new StringBuilder(256);
sb.append(arr[0]);
//if (arr.length == 1) return sb.toString();
for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);
return sb.toString();
}
Then you can do:
Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
Java 8 Solution would be like this:
Use with AIStream.of(1,2,3,4).map(String::valueOf).collect(Collectors.joining("-"))
Just multiply and add the digits:
int result = 1000 * arr[3] + 100 * arr[2] + 10 * arr[1] + arr[0];
Or, if you need it to work for any length of array (up to the number of digits in Integer.MAX_VALUE):
int result = 0;
for (int i = arr.length - 1; i >= 0; --i) {
result = 10*result + arr[i];
}
I would use a stringbuilder.
StringBuilder builder = new StringBuilder();
builder.append(arr[3]);
builder.append(arr[2]);
builder.append(arr[1]);
builder.append(arr[0]);
System.out.println("Combined value is: " + Integer.parseInt(builder.toString());
You can't add them directly, you have to make a new array and then copy each of the arrays into the new one. System.arraycopy is a method you can use to perform this copy.
int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
This will work regardless of the size of array1 and array2.
Here a simple function that use variable arguments:
final static
public int[] merge(final int[] ...arrays ) {
int size = 0;
for ( int[] a: arrays )
size += a.length;
int[] res = new int[size];
int destPos = 0;
for ( int i = 0; i < arrays.length; i++ ) {
if ( i > 0 ) destPos += arrays[i-1].length;
int length = arrays[i].length;
System.arraycopy(arrays[i], 0, res, destPos, length);
}
return res;
}
To use:
int[] array1 = {1,2,3};
int[] array2 = {4,5,6};
int[] array3 = {7,8,9};
int[] array1and2and3 = merge(array1, array2, array3);
for ( int x: array1and2and3 )
System.out.print( String.format("%3d", x) );
If you wanna do it using reduce convert Integers into Strings and then use Accumulator function
Optional<String> result = numbers.stream().map(i->i.toString()).reduce((i,j)->i+", "+j);
Or you can simply use Collectors.joining
String str = numbers.stream().map(i->i.toString()).collect(Collectors.joining(", "));
You can use String.join for that. It joins given Iterable of CharSequence with specified delimiter (", " in your case):
List<String> strings = numbers.stream().map(Object::toString).collect(Collectors.toList())
String concatenated = String.join(", ", strings)