To deep copy an array, you can use System.arraycopy(..)
http://developer.android.com/reference/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

System.arraycopy(b, 0, a, 0, 3);

but for a float array of length 3, you can simply write
a[0]=b[0];
a[1]=b[1];
a[2]=b[2];

or
for (i=0; i<3; i++) a[i] = b[i];

I've never tried to profile which of the 3 ways is actually the
fastest on a real Android device. If someone has the time, please try
all 3 in a tight loop and report back :-)

(there is also
for(i=3; i-->0;) a[i] = b[i];
which may be even faster?)

Note: all assume that you have initialized a already beforehand,
a = new float[3];

Peli

On Aug 25, 8:24 am, Rud <[email protected]> wrote:
> Hi,
>
> The code is originally from my Blog. The reason for the clone is
> explained there.
>
> I admit to not having worked with Java enough to have the deep
> understanding of how containers deal with objects. In my application I
> ran into a problem trying to make a copy of an array and had to write
> my own copy.
>
> I had a set of data that gets consumed while the app runs. I wanted to
> reset that data at times. I put the data in an array and then cloned
> it to the working array. I was surprised when the original data got
> changed when the cloned data was changed. I also realized that the
> sensor code wasn't doing what I thought it was but haven't taken the
> time to adjust it.
>
> How in Java do you actually make a deep copy of an array?
>
> Rudhttp://mysticlakesoftware.blogspot.com/
>
> On Aug 21, 7:07 am, Peli <[email protected]> wrote:
>
> > > .... = event.values.clone();
>
> > Note that this creates new objects constantly that have to be garbage
> > collected later. Depending on the kind of application, it may be
> > better to just copy the values into a persisting array.
>
> > Peliwww.openintents.org
>
> > On Aug 21, 4:06 am, mscwd01 <[email protected]> wrote:
>
> > > Heres my code, which works:
>
> > > public void onSensorChanged(SensorEvent event) {
>
> > >         Sensor sensor = event.sensor;
> > >         int type = sensor.getType();
> > >         switch (type) {
> > >                 case Sensor.TYPE_MAGNETIC_FIELD:
> > >                         mags = event.values.clone();
> > >                         isReady = true;
> > >                     break;
> > >                 case Sensor.TYPE_ACCELEROMETER:
> > >                     accels = event.values.clone();
> > >                     break;
> > >                 case Sensor.TYPE_ORIENTATION:
> > >                     orients = event.values.clone();
> > >                     break;
> > >             }
>
> > >             if (mags != null && accels != null && isReady) {
> > >                 isReady = false;
>
> > >                     SensorManager.getRotationMatrix(R, I, accels, mags);
> > >                     SensorManager.remapCoordinateSystem(R,
> > > SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, outR);
> > >                     SensorManager.getOrientation(outR, values);
>
> > >                                 azimuth = 
> > > getAzimuth(-convert.radToDeg(values[0]));
> > >                                 pitch= convert.radToDeg(values[1]);
> > >                                 roll = -convert.radToDeg(values[2]);
>
> > >             }
>
> > >         }
>
> > > On Aug 21, 2:39 am, Mike Collins <[email protected]> wrote:
>
> > > > I have a sensor event handler that gets fired and I have no problem
> > > > getting and processing acceleration readings. However this code
> > > > always fails.
>
> > > >                 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
> > > >                 {
> > > >                         float[] geomagnetic = new float[3];
> > > >                         geomagnetic[0] = geomagnetic[1] = 
> > > > geomagnetic[2] = 0;
>
> > > >                         float[] r = new float[9];
> > > >                         float[] I = new float[9];
> > > >                         boolean b = SensorManager.getRotationMatrix(r, 
> > > > I, event.values.clone
> > > > (), event.values.clone());
> > > >                         if ( ! b)
> > > >                         {
> > > >                                 Log.e(LOG_TAG, "getRotationMatrix 
> > > > failed");
> > > >                                 return;
> > > >                         }
> > > > ...
>
> > > > tia,
> > > >   mike- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to