| Author |
How do i convert string array into long array in java?
|
Birla Murugesan
Ranch Hand
Joined: Nov 25, 2008
Posts: 64
|
|
How do i convert string array into long array in java?
what i am having is:
i want to convert this into Long Arrays
please help in this regards.
Thanks in advance,
Birla
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 11225
|
|
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:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Birla Murugesan
Ranch Hand
Joined: Nov 25, 2008
Posts: 64
|
|
Without iterating the array,Is there is any other way to do it.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 17259
|
|
Simply put, no. Whatever solution you use, in the end you will still need to change each separate string element into a long.
Jesper, I'd suggest one slight modification: use long instead of Long:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 13408
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 17259
|
|
Henry Wong wrote:
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).
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 11225
|
|
Rob Prime wrote:Jesper, I'd suggest one slight modification: use long instead of Long:
Ok, but the OP specifically asked for a Long array. This line of your modified version does not work:
It should be:
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 17259
|
|
Jesper Young wrote:
Rob Prime wrote:Jesper, I'd suggest one slight modification: use long instead of Long:
Ok, but the OP specifically asked for a Long array.
Actually, the OP asked for both:
How do i convert string array into long array in java?
i want to convert this into Long Arrays
This line of your modified version does not work:
It should be:
Guess my mental search-and-replace worked too good
|
 |
 |
|
|
subject: How do i convert string array into long array in java?
|
|
|