commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dean <dean.w.parsh...@gmail.com>
Subject RE: [math] complex[][] clone problem
Date Thu, 04 Nov 2010 18:03:37 GMT
iirc clone by default returns a shallow copy, so what you end up doing
is creating a refrence to the object in question. I know that is the
default behaviour with non-primatives. You should override clone to
perform a deep copy if thats what you need, or use a copy constructor.

~Dean

On Thu, 2010-11-04 at 17:54 +0000, Haswell, Joe wrote:
> I think the issue is with how Array.clone() operates on multi-dimensional arrays.  Cloning
a multi-dimensional array will create a new multi-dimensional array with the same dimensions
and the same sub-arrays as elements, so Complex[][] d = ... etc. Complex[][] c = d.clone();
c[0] == d[0] should return true. 
> 
> Joe H. | HP Software
> 
> -----Original Message-----
> From: Cuong P. Nguyen [mailto:cnguyen7@iit.edu] 
> Sent: Thursday, November 04, 2010 11:27 AM
> To: user@commons.apache.org
> Subject: [math] complex[][] clone problem
> 
> Hi,
> 
>  
> 
> I might have a trivial problem and hope some of you can explain. Here is the
> source code I used, where I wanted to clone c Complex[][] object to
> Complex[][] d. The problem is when c matrix is updated, d is also updated
> which I did not expect. I expect d to stay the same when c is updated.
> 
>  
> 
> Thanks in advance,
> 
> Cuong
> 
>  
> 
> 
> 
> 
> 
> Code:
> 
>                   System.out.println("Test 1.3");
> 
>                   Complex[][] c = new Complex[][]{{new Complex(1,2), new
> Complex(3,4)},
> 
>                               {new Complex(5,6), new Complex(7,8)}};
> 
>                   Complex[][] d = c.clone();
> 
>                   for (int i=0;i<2;i++) {
> 
>                         for (int j=0;j<2;j++) {
> 
>                               System.out.println("
> c["+i+"]["+j+"]="+format.format(c[i][j]));
> 
>                         }
> 
>                         
> 
>                   }
> 
>                   for (int i=0;i<2;i++) {
> 
>                         for (int j=0;j<2;j++) {
> 
>                               System.out.println("
> d["+i+"]["+j+"]="+format.format(d[i][j]));
> 
>                         }
> 
>                         
> 
>                   }
> 
>                   System.out.println("Test 1.4");
> 
>                   c[0][0]=c[0][0].multiply(100);
> 
>                   for (int i=0;i<2;i++) {
> 
>                         for (int j=0;j<2;j++) {
> 
>                               System.out.println("
> c["+i+"]["+j+"]="+format.format(c[i][j]));
> 
>                         }
> 
>                         
> 
>                   }
> 
>                   for (int i=0;i<2;i++) {
> 
>                         for (int j=0;j<2;j++) {
> 
>                               System.out.println("
> d["+i+"]["+j+"]="+format.format(d[i][j]));
> 
>                         }
> 
>                         
> 
>                   }
> 
>  
> 
> And the results are:
> 
> Test 1.3
> 
>  c[0][0]=1.0000000 + 2.0000000i
> 
>  c[0][1]=3.0000000 + 4.0000000i
> 
>  c[1][0]=5.0000000 + 6.0000000i
> 
>  c[1][1]=7.0000000 + 8.0000000i
> 
>  d[0][0]=1.0000000 + 2.0000000i
> 
>  d[0][1]=3.0000000 + 4.0000000i
> 
>  d[1][0]=5.0000000 + 6.0000000i
> 
>  d[1][1]=7.0000000 + 8.0000000i
> 
> Test 1.4
> 
>  c[0][0]=100.0000000 + 200.0000000i
> 
>  c[0][1]=3.0000000 + 4.0000000i
> 
>  c[1][0]=5.0000000 + 6.0000000i
> 
>  c[1][1]=7.0000000 + 8.0000000i
> 
>  d[0][0]=100.0000000 + 200.0000000i
> 
>  d[0][1]=3.0000000 + 4.0000000i
> 
>  d[1][0]=5.0000000 + 6.0000000i
> 
>  d[1][1]=7.0000000 + 8.0000000i
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message