File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Storing objects un an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing objects un an ArrayList " Watch "Storing objects un an ArrayList " New topic

Storing objects un an ArrayList

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 70

ResultSet xxx_AppCode = statement.executeQuery(
ArrayList<Object> applCodes=new ArrayList<Object>();

while ( {
// create the Application Code Object
ApplicationCode applicationCode = new ApplicationCode();
applicationCode.setApplCD(xxx_AppCode.getString("code "));
applicationCode.setAPPLDESCTX(xxx_AppCode.getString("DESC "));

applCodes.add(applicationCode); }

What I'm trying to do above is read a DB table create an object for each row it brings back (2 cols each row) so I can store each row as an object in an ArrayList, Not sure if the above is the correct way ? If it is how do I read it back, any examples , commenst etc. are appreaciated. Thank You
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2452
PLease edit your question, repaste the code so it contains formatting and then select the code and use the Code button so the code will be nicely formatted in your question.

Not sure if the above is the correct way ?

Looks reasonable

comments etc. are appreaciated.

Use generics on the Arraylist to store a specific object type, not just any object:

how do I read it back

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Better yet, program to an interface, not an implementation:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 70

Thanks to all who replied your replies worked perfect ! But I left a few important points out.

I start out in Class A1 and create an object by calling Class APPL which does the read of the table and creates the Arraylist storing the object. then it returns the Arraylist object to Class A1. I store it in an Attribute to pass it to a JSP . I then do a foreach to display the values in the object. This is the part I can't get to work now, but I'm close. Becuase it displays the memory addresss of each item in the array. I only want to display the DESC (2nd value ) in each ArrayList Occurrence. I will use both(code & desc) later on though. Am I on the right path , please show an example. Thanks Again

Class A1

Class APPL

Class A1

//store the need data for the session


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Then you'll want to implement a "public String toString()" method in your ApplicationCode which returns a suitable string for displaying in that position.

(By the way I edited your last post to use the "Code" tags. See that button which says "Code" above the box where you're posting?)

And in future, if you're going to design classes which can be used in JSTL and EL expressions like (which is definitely a good thing) then you should be designing them as Java beans. That puts certain requirements on the names of getter methods, for example, so that they can be used as properties in EL.

For example if your ApplicationCode class had a "public String getDescription()" method which returned a description for the object, then your EL expression to produce the description of an object which is referred to by a EL variable "XXX" would be like this:

However the methods I saw in your post don't conform to the Java Bean specification, which is why I suggested overriding toString() as a stopgap.
I agree. Here's the link:
subject: Storing objects un an ArrayList
It's not a secret anymore!