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();
Convert String to int array in java - Stack Overflow
reduce an array to string
Converting int[,] array to string[,] array?
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);
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 come from a Ruby background, and am trying to get my feet wet using functional methods in Java. I'm having trouble understanding why the following example won't reduce an array of integers down to a string.
import java.util.Arrays;
import java.util.List;
import java.util.stream.*;
public class ReduceTest {
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
IntStream stream = [Arrays.stream](https://Arrays.stream)(numbers);
String output = stream.reduce("",(int string, int number) -> string + Integer.toString(number));
}
}I'm getting the error:
The method reduce(int, IntBinaryOperator) in the type IntStream is not applicable for the arguments (String, (int string, int number) ->
{})
I don't understand why it's expecting an IntBinaryOperator, and don't really know what that is. From the examples I have seen, I would expect to pass a string to an accumulator.
Any thoughts/suggestions are appreciated, also would like to know the "Java" way of converting an array down to a string.