wood burning stoves 2.0*
The moose likes Java in General and the fly likes How do i convert string array into long array in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do i convert string array into long array in java?" Watch "How do i convert string array into long array in java?" New topic
Author

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

Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Posts: 66

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 66

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i convert string array into long array in java?
 
Similar Threads
String array to arraylist
convert an array of String to char
convert a String to Long
Strings and arrays
error please help