Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Birla Murugesan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Without iterating the array,Is there is any other way to do it.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic