Question

Yes I've googled, Yes I've read, Yes I'm still stuck...

I have an ArrayList of objects in ClassB that I'm returning to an ArrayList of objects in ClassA by calling a method from A that's in B. (Same return type: ) In CLassB, I do all the processing and storage of the object attributes into an Array of objects of the class type.

ClassB:

ClassB object = new ClassB();
ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){
   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  

}

ClassA:

ArrayList<ClassB> arrayOfObjects = ClassBObject.method();



String[] columns = {"Column1","Column2","Column3", "Column4", "Column5"};

      DefaultTableModel tableModel = new DefaultTableModel(columns, 0);

      JTable table = new JTable(tableModel);

      int i = 0;
      while(i < arrayOfObjects.size()) {

         //v for variable       
         String v1 = arrayOfObjects .get(i).attribute1;
         String v2 = arrayOfObjects .get(i).attribute2;
         String v3 = arrayOfObjects .get(i).attribute3;
         String v4 = arrayOfObjects .get(i).attribute4;
         String v5 = arrayOfObjects .get(i).attribute5;

         Object[] row = {v1,v2,v3,v4,v5};

         tableModel.addRow(row);
         i++;
      }
         JFrame frame = new JFrame("Title of Table");

            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            JScrollPane tableContainer = new JScrollPane(table);

            panel.add(tableContainer, BorderLayout.CENTER);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

Issue: With the above code, the table only displays the last object which is repeated as many times as the while loop executes. I'd like to extract the attributes from each object from the array of objects where the counter is used to match the element of the array of objects.

I hope I was detailed enough to provide enough background to get to the root of the problem. Thanks in advance!

Was it helpful?

Solution

OK, now we see your problem (possibly)!

ClassB object = new ClassB();
ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){
   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  
}

You don't recreate a new object each time in the loop!

Instead try:

// ***** don't create object *once* outside of the while loop!
//  ClassB object = new ClassB();

ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){

   // *** instead create a new one for each iteration of the loop!
   ClassB object = new ClassB(); // *****************

   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top