aspose file tools*
The moose likes Beginning Java and the fly likes fetch from string array into collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "fetch from string array into collection" Watch "fetch from string array into collection" New topic
Author

fetch from string array into collection

sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 283
I have following program



It is giving output as

[[hello, world],
[Goodbye, planet]]


I want to take out

hello and world
similarly
Goodbye, planet

and put it into some collection etc
so that later i can loop through and query database using
hello as 'username' and world as 'password'

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

API explains why you get that result here. Check the description of deepToString method.
On the other hand, what you want to do is best accomplished using double for loop, in each iteration of inner one you can get one element, and perform whatever you want with it.


The quieter you are, the more you are able to hear.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

Kemal Sokolovic wrote:
On the other hand, what you want to do is best accomplished using double for loop, in each iteration of inner one you can get one element, and perform whatever you want with it.


It is probably more interesting to code it recursively -- as it can be use to flatten out any depth of arrays of arrays.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Henry Wong wrote:It is probably more interesting to code it recursively -- as it can be use to flatten out any depth of arrays of arrays. Henry

Agree, but I don't think recursion is something that beginners should play with, and based on the question I think sai rama krishna is one of those.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Why should beginners not play with recursion?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Campbell Ritchie wrote:Why should beginners not play with recursion?

Based on what I've seen so far, they find it difficult to catch up with, and even more to implement. Programs usually end up with with stack overflow, end coders being more confused.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Kemal Sokolovic wrote:I don't think recursion is something that beginners should play with

I disagree, some people are very much at home when thinking from recursion point of view... Some find it even more simpler than looping...


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

salvin francis wrote:I disagree, some people are very much at home when thinking from recursion point of view... Some find it even more simpler than looping...

Well, that's your opinion and I must disagree with it. Giving a recursive algorithm to someone who is not comfortable with iterating over two dimensional array doesn't seem to me as a good idea.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

salvin francis wrote:I disagree, some people are very much at home when thinking from recursion point of view... Some find it even more simpler than looping...

I think it depends on your training. Many people with a strong Maths background find recursion quite straightforward; on the other hand, duffers like me find it quite tough. And since good programmers program not only for themselves, but for the person who has to come after them, I'd say there's an argument for not using recursion unless there's a strong reason for doing so (eg, divide and conquer algorithms).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I agree with that. There are some problems that are naturally solved with recursion, e.g. quicksort or mergesort algorithm. On the other hand, a task such as printing contents of an array (even a multi dimensional one) implemented recursively would just look more elegant at the expence of efficiency (each time entire frame is pushed onto stack, which consumes both time and memory) and that's not the thing you would want to waste for such trivial tasks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: fetch from string array into collection