So I have this "list" of ints. It could be a Vector
, int[]
, List<Integer>
, whatever.
My goal though is to sort the ints and end up with a String[]
. How the int array starts out as is up in the air.
ex: Start with:{5,1,2,11,3}
End with: String[] = {"1","2","3","5","11"}
Is there anyway to do this without a for loop? I have a for loop now for collecting the ints. I would rather skip doing another for loop.
Arrays. toString(int[]) method returns a string representation of the contents of the specified int array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]").
To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings.
Convert Array to String. Sometimes we need to convert an array of strings or integers into a string, but unfortunately, there is no direct method to perform this conversion. The default implementation of the toString() method on an array returns something like Ljava. lang.
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));
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