commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cuong P. Nguyen" <cnguy...@iit.edu>
Subject [math] complex[][] clone problem
Date Thu, 04 Nov 2010 17:27:04 GMT
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


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message