So the String array users now contains strings that contain all digits? You'll need to create a Long array of the same length as the String array and then iterate through the String array to fill the elements of the Long array:
Birla Murugesan wrote:
Without iterating the array,Is there is any other way to do it.
Rob Prime wrote:Simply put, no. Whatever solution you use, in the end you will still need to change each separate string element into a long.
A more complicated answer ... Maybe. You already iterated once through the string, in order to split it into an array. Technically, there is a way to do without iterating through the array -- you can skip generating the array in the first place.
Instead of using split(), you can use find(), to get each "long" string element, in order to convert to a long -- in one pass.
Now, of course, whether this saves any time, is unclear.
Birla Murugesan wrote:
Without iterating the array,Is there is any other way to do it.
Rob Prime wrote:Simply put, no. Whatever solution you use, in the end you will still need to change each separate string element into a long.
A more complicated answer ... Maybe. You already iterated once through the string, in order to split it into an array. Technically, there is a way to do without iterating through the array -- you can skip generating the array in the first place.
Instead of using split(), you can use find(), to get each "long" string element, in order to convert to a long -- in one pass.
Now, of course, whether this saves any time, is unclear.
Henry
I agree that splitting and parsing in one go would remove the need for a second iteration. And I doubt that using a Pattern / Matcher combination would be slower - because that's just what String.split uses - it delegates to Pattern.split which uses a Matcher. This splitting a string into longs would be something like this (code based on Pattern.split):
Of course that would require another iteration to turn the List<Long> into a Long[] or long[], but the List<Long> should be fine for most cases.
But the initial question was: "How do i convert string array into long array in java?" and that's impossible without a second iteration (I count recursion as iterating in this case).