Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help adding and retrieving Arrays in an ArrayList

Status
Not open for further replies.

viper2003

Programmer
May 5, 2003
7
US
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.
 
I wrote a test program using your code, but hardcoding an array of data rather than using a resultSet. My test program ran fine.

Your example doesn't show where the object cms_utilities is being created. Could you have more than one of these objects (one global, one local)?

It would be helpful if you could create a similar test program to mine using your source code, verify that it indeed fails, and then post that entire test program.

Here's my test program:

Code:
import java.util.*;

public class Class1 
{
   public Class1()
   {
      String[] vendor = {&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;};
      String[] start_date = {&quot;1101&quot;,&quot;1102&quot;,&quot;1103&quot;};
      String[] end_date = {&quot;1201&quot;,&quot;1202&quot;,&quot;1203&quot;};

      Cms_utilities cms_utilities = new Cms_utilities();
      for (int i=0; i<3; i++)
      {
         String[] details = new String[3];
         details[0] = vendor[i];
         details[1] = start_date[i];
         details[2] = end_date[i];
      
         // 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);

      }

      //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);
         System.out.println(details_array[0] + &quot;, &quot; + details_array[1] + &quot;, &quot; + details_array[2]);
         counter++;
      }   
   }

   public static void main(String[] args)
   {
      Class1 class1 = new Class1();
   }
}

class Cms_utilities
{
   //arraylist decalred as global in the class
   private ArrayList detail_array_list=new ArrayList();

   public void BookingsDetailSetRow(String[] detail_array)
   {
      //populate Arraylist
      detail_array_list.add(detail_array);
   }

   public ArrayList getBookingsDetail()
   {  
      //return populated ArrayList
      return detail_array_list;
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top