Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problem adding Arrays to an ArrayList in a loop

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help with an ArrayList problem, I will write the code and expalin it and at the end will describe the problem.
JSP
=========================================================================
1)I get a java.sql.ResultSet from an ejb to my jsp,
resultSet=myejb(vendor,start_date,end_date);

2)I set the individual columns in an array and pass the array to a bean method
that justs adds the array to an ArrayList
//check if any records exist in the ResultSet
legit=resultSet.next();
while(legit){
//set the ResultSet coulmns in the details Array
details[0]=resultSet.getString("ITEM_NO");details[1]=resultSet.getString("QUANTITY_NO");details[2]=resultSet.getString("WHSE_NO");details[3]="description goes here";
//pass the populated array to a method in a class file, this class file just
adds the Array to an Arraylist(scroll to end for references)(ref#1)
cms_utilities.BookingsDetailSetRow(details);
//check for more records in the ResultSet
legit=resultSet.next();
}
3) and then I just want to print out the Array list
//declare a new Array
ArrayList BookingsDetails= new ArrayList();
//call a method in the cms_utilities class (ref#2) that returns the populated ArrayList, in step 2
BookingsDetails=cms_utilities.getBookingsDetail();
int counter=0;
int ArrayListSize=BookingsDetails.size();
//loop though the Arraylist
while(counter<ArrayListSize){
//convert Arraylist's rows into an Array to print them
String[] details_array= (String[]) BookingsDetails.get(counter);
out.print(details_array[0]);
out.print(details_array[1]);
out.print(details_array[2]);
out.print(details_array[3]);
counter++;
}
========================================================================
cms_utilities class
//arraylist decalred as global in the class
ArrayList detail_array_list=new ArrayList();
(ref#1)
public void BookingsDetailSetRow(String[] detail_array)
{
//populate Arraylist
detail_array_list.add(detail_array);
}
(ref#2)
public ArrayList getBookingsDetail()
{
//return populated ArrayList
return detail_array_list;
}
===========================================================================
The problem is that even though I am adding the correct elements to the ArrayList, I confirmed this because I did a printout of every array Array I passed to the method and it is the correct information being set.
However when I get the populated ArrayList, I get only the last set of data in all the records, in other words I get the correct number of rows but they are all populated with the last Array added to the ArrayList.
I am really certain it is not a loop or something I am missing somewhere, I have spent a day already checking all the details.
Another thing I have noticed is that it all works well if I add one record at a time, in other words, if I take the loop out and add one record at a time the ArrayList will keep on incrementing the correct data and diplay it in the correct manner, so my hunch is that somehow when I add records in the loop it looses all the information except the last record.
Any help will be greatly appreciated.
[ January 20, 2004: Message edited by: Sergio Barreros ]
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not specially related to JSP, but is a basic feature of Java. A list (or any other collection), just contains references to whatever you give it. It does not "copy" or "clone" the data.
So, when you add your array to the list, it just adds a reference to the same array each time. Sure, each time the contents of the entries of the array are different, but the list doesn't care. It just knows how to find the array when it needs to.
To solve this problem you need to use new or clone() to create a distinct object for each entry in the list. You can either do this in the ResultSet loop, or inside your BookingDetailsSetRow method.
One suggestion, though. I feel that by using an array to collect your row items you are losing information that you know about the returned data. I reckon that your code would be both simpler and more readable if you used an object for this:

Does that make sense?
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I somehow missed this one when it was first posted. Frank is, of course, correct. Moving to the Java in General(intermediate) forum.
 
Sergio Barreros
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Frank and you are right, that is much more readeable, but I am just starting to use ArrayLists and for some reason I got the impression only Arrays could be added to it.
Thanks again
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic