What you want is the Arrays.toString(int[]) method:
import java.util.Arrays;
int[] array = new int[lnr.getLineNumber() + 1];
int i = 0;
..
System.out.println(Arrays.toString(array));
There is a static Arrays.toString helper method for every different primitive java type; the one for int[] says this:
Answer from Sbodd on Stack Overflowpublic static String toString(int[] a)Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (
"[]"). Adjacent elements are separated by the characters", "(a comma followed by a space). Elements are converted to strings as byString.valueOf(int). Returns"null"ifais null.
What you want is the Arrays.toString(int[]) method:
import java.util.Arrays;
int[] array = new int[lnr.getLineNumber() + 1];
int i = 0;
..
System.out.println(Arrays.toString(array));
There is a static Arrays.toString helper method for every different primitive java type; the one for int[] says this:
public static String toString(int[] a)Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (
"[]"). Adjacent elements are separated by the characters", "(a comma followed by a space). Elements are converted to strings as byString.valueOf(int). Returns"null"ifais null.
Very much agreed with @Patrik M, but the thing with Arrays.toString is that it includes "[" and "]" and "," in the output. So I'll simply use a regex to remove them from outout like this
String strOfInts = Arrays.toString(intArray).replaceAll("\\[|\\]|,|\\s", "");
and now you have a String which can be parsed back to java.lang.Number, for example,
long veryLongNumber = Long.parseLong(intStr);
Or you can use the java 8 streams, if you hate regex,
String strOfInts = Arrays
.stream(intArray)
.mapToObj(String::valueOf)
.reduce((a, b) -> a.concat(",").concat(b))
.get();
Here's a stream version which is functionally equivalent to khelwood's, yet uses different methods.
They both create an IntStream, map each int to a String and join those with commas.
They should be pretty identical in performance too, although technically I'm calling Integer.toString(int) directly whereas he's calling String.valueOf(int) which delegates to it. On the other hand I'm calling IntStream.of() which delegates to Arrays.stream(int[]), so it's a tie.
String result = IntStream.of(intArray)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));
This should do
String arrAsStr = Arrays.toString(intArray).replaceAll("\\[|\\]", "");
After Arrays toString, replacing the [] gives you the desired output.
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, " - ");
Java 8 Solution would be like this:
Use with AIStream.of(1,2,3,4).map(String::valueOf).collect(Collectors.joining("-"))
Simplest performant approach is probably StringBuilder:
StringBuilder builder = new StringBuilder();
for (int i : array) {
builder.append(i);
}
String text = builder.toString();
If you find yourself doing this in multiple places, you might want to look at Guava's Joiner class - although I don't believe you'll be able to use it for primitive arrays. EDIT: As pointed out below, you can use Ints.join for this.
int[] x = new int[] {3,4,5};
String s = java.util.Arrays.toString(x).replaceAll("[\\,\\[\\]\\ ]", "")
Update
For completeness the Java 8 Streams solution, but it isn't pretty (libraries like vavr would be shorter and faster):
String s = IntStream.of(x)
.mapToObj(Integer::toString)
.collect(Collectors.joining(""));
Yes. However, there is no Collectors.joining for a Stream<Integer>; you need a Stream<String> so you should map before collecting. Something like,
System.out.println(i.stream().map(String::valueOf)
.collect(Collectors.joining(",")));
Which outputs
1,2,3,4,5
Also, you could generate Stream<Integer> in a number of ways.
System.out.println(
IntStream.range(1, 6).boxed().map(String::valueOf)
.collect(Collectors.joining(","))
);
It is very easy with the Apache Commons Lang library.
Commons Lang
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
String str = org.apache.commons.lang.StringUtils.join(list, ","); // You can use any delimiter
System.out.println(str); // Output: 1, 2, 3, 4, 5, 6, 7
Java 8 Solution
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
String joinedList = list.stream().map(String::valueOf).collect(Collectors.joining(","));
System.out.println(joinedList);
Using Java 8 you can do this in a very clean way:
String.join(delimiter, elements);
This works in three ways:
1) directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");
2) using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3) using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
If you're on Android you can TextUtils.join(delimiter, tokens)