This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to convert to statements to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to convert to statements to an array" Watch "how to convert to statements to an array" New topic
Author

how to convert to statements to an array

Bob Dunn
Greenhorn

Joined: Jun 23, 2011
Posts: 12
how would i convert this to an array...

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't expect you mean to ask how to turn that code into an array:




So what, exactly is it that you want to convert to an array?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I guess that what you want is retrieve multiple records from the database, and store the results in an array.

An array is not the best suited collection to do this, because arrays in Java have a fixed size, which is set at the moment you create an array, and here you don't know in advance how many records you are going to retrieve from the database.

So instead of an array, it would be better to use a collection class such as ArrayList, which can grow dynamically. To put the results of the query in an ArrayList of User objects, you could do something like this:

You can learn more about collection classes such as ArrayList in the Collections Tutorial.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bob Dunn
Greenhorn

Joined: Jun 23, 2011
Posts: 12
Thanks for the suggestion, I think the arraylist is a better way to go.

so this is what I came up with but am getting an error on the statement, users.add(Selectuser); It can't find the symbol 'selectUser' but it is there. What am I doing wrong?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bob Dunn wrote:It can't find the symbol 'selectUser' but it is there. What am I doing wrong?

Actually the error message mentions the symbol 'Selectuser' rather than the symbol 'selectUser'. Those are two different things -- case counts in Java programming.

But anyway you say the symbol is "there". What exactly did you mean by "there"? Could you perhaps point to a line number in your post where you believe you have declared that symbol?
Bob Dunn
Greenhorn

Joined: Jun 23, 2011
Posts: 12
I thought it is on line 39 but it obviouslyis not or else I wouldn't be getting the error. I'm just trying to add the entries to the arraylist.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to convert to statements to an array
 
Similar Threads
String array to arraylist
ResultSet to Array
dispatcher.forward: sometimes it does not work!
Adding code to return an ArrayList of objects
how to convert byte array into short??