• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Without iterating the array,Is there is any other way to do it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic