This week's book giveaway is in the Flex forum.
We're giving away four copies of Flex 4 in Action and have Tariq Ahmed, Dan Orlando, John C. Bland II & Joel Hooks on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
Author

How do i convert string array into long array in java?

Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Messages: 58


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 Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7852

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 6.0 API documentation
Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Messages: 58


Without iterating the array,Is there is any other way to do it.
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8816

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
Bartender

Joined: Sep 28, 2004
Messages: 10005

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 Prime
Bartender

Joined: Oct 27, 2005
Messages: 8816

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).

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7852

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:

Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8816

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

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
JProfiler
Get rid of your performance problems and memory leaks!

.