1

Sorry if the title isn't very descriptive, I don't really know how to phrase it.

I have three separate ArrayList like this:

Object m1 = new Object();
Object m2 = new Object();
Object m3 = new Object();

Which all contain an ArrayList of type object, Objects

and a function which multiplies the values by some integer value:

public void multiply(int n)  
{ 
    ArrayList<object> new_objects = new ArrayList<object>();
    for (int i=0;i<objects.size();i++) {
        Object o = objects.get(i); 
        o.setValue(o.getValue()*n);
        new_objects.add(e); //The multiplication and assignment works as intended here
    }
    this.objects=new_objects;
} 

I call this function using, for example:

Object m2_product = m1.multiply(3);
Object m3_product = m1.multiply(3);

The purpose of which is to end up with three ArrayLists with separate values:

m1 with the original values,

m2 with the original values multiplied by 3, and

m3 with the original values multiplied by 9.

My issue is that, upon printing these ArrayLists values, all of them are multiplied by 9. they continued to be updated after already being defined. What is wrong with my code that makes these variables continue to update after their assignment?

This is for a coding project at university, so I'm not looking for an exact answer, rather just the reason that it does not work. All of the code besides what is within the multiply function have been provided, and cannot be changed. Something must be wrong with the multiply function but I just cannot figure out why.

Thank you.

Conch
  • 56
  • 5

0 Answers0