Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fetch from string array into collection

 
sai rama krishna
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should beginners not play with recursion?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic