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();
java - Convert int array to string variable - Stack Overflow
Convert String to int array in java - Stack Overflow
How to put a string into an array of integer?
Java- Converting a string of numbers separated by spaces into an array?
int[] nums = {5,1,2,11,3}; //List or Vector
Arrays.sort(nums); //Collections.sort() for List,Vector
String a=Arrays.toString(nums); //toString the List or Vector
String ar[]=a.substring(1,a.length()-1).split(", ");
System.out.println(Arrays.toString(ar));
UPDATE:
A shorter version:
int[] nums = {-5,1,2,11,3};
Arrays.sort(nums);
String[] a=Arrays.toString(nums).split("[\\[\\]]")[1].split(", ");
System.out.println(Arrays.toString(a));
Use a Stream which is available from Java 8. To get a Stream instance with "list" of ints:
- For
int[]IntStream intStream = Arrays.Stream(nums);orStream<Integer> intStream = Arrays.Stream(nums).boxed();if you need the same class as bottom one.
- For any classes with
Collection<Integer>interface (ex.Vector<Integer>,List<Integer>)Stream<Integer> intStream = nums.stream();
Finally, to get a String[]:
String[] answer = intStream.sorted().mapToObj(String::valueOf).toArray(String[]::new);
int[] arr = new int[] { 1, 0, 1, 0 };
StringBuilder sb = new StringBuilder(arr.length);
for (int i : arr) {
sb.append(i);
}
String s = sb.toString(); // 1010
If you don't mind having brackets around your string, and your numbers separated by a comma, you can use Arrays.toString as suggested by others.
Probably Arrays.toString(int[]) is what you are looking for.
final int[] sample = new int[] {1, 2, 3};
final String arrayStr = Arrays.toString (sample);
System.out.println (arrayStr);
This should print [1, 2, 3].
Here is a fiddle for it.
String arr = "[1,2]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {
//NOTE: write something here if you need to recover from formatting errors
};
}
Using Java 8's stream library, we can make this a one-liner (albeit a long line):
String str = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
int[] arr = Arrays.stream(str.substring(1, str.length()-1).split(","))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(arr));
substring removes the brackets, split separates the array elements, trim removes any whitespace around the number, parseInt parses each number, and we dump the result in an array. I've included trim to make this the inverse of Arrays.toString(int[]), but this will also parse strings without whitespace, as in the question. If you only needed to parse strings from Arrays.toString, you could omit trim and use split(", ") (note the space).
I have an array of integers and need to replace some of them with strings of text. All my attempts have been in vain.
int [] myArray = {0, 1, 2, 3, 4};
myArray[0] = "string";
This method works for other integers but not for strings. Is it because the array can only ever contain integers? Is there a way around this? My quick googling hasn't answered my question so I just thought I'd ask here. Thanks for any help! :)