Hi all!
I have a array problem I can not figure out.
I have an array that I copy in a method and returns the copy,
but I do not get a copy of the array. I just get strange values.

public class AClass
{
    private float[] original = new float[5];
    private float[] finalyStore = new float[5];

    public AClass()
    {
        //init orginal
        original[0] = .....
        finalyStore = testToCopy();
    }

    private float[] testToCopy()
    {
        float[] copyOfOriginal = new float[5];
        for(int i=0; i < original.length; i++)
        {
            copyOfOriginal[i] = original[i];
        }
        return copyOfOriginal;
    }
}

The code is just a short snippet to clear out what I mean.
What comes out in "finalyStore" is not the same as "original".
If I just copy the entire array it works fine:

copyOfOriginal = original;
return copyOfOriginal;

What am I missing? Where do I think wrong?

Many thanks in advance!
Marcux

Recommended Answers

All 4 Replies

The code looks ok to me. What values do you have in origianl array?

Just one thing to clear it out:
If you have two arrays declared on the class level, then you do not need to use return and creating other arrays in methods inside of this class. Because those two array (original and finalyStore) are accessed in all the methods inside of the class.

So you can do:

public class AClass
{
    private float[] original = new float[5];
    private float[] finalyStore = new float[5];
 
    public AClass()
    {
        //init orginal
        original[0] = .....
        finalyStore = testToCopy();
    }
 
    private void testToCopy()
    {
        for(int i=0; i < original.length; i++)
        {
            finalyStore[i] = original[i];
        }
    }
}

But if you have set the code as this you dont even need an additional method (testToCopy). But maybe there is a bit different code you have (as you have stated).

Anyway, you code should work too. I dont know what might be wrong at all (it actually cannot be anything wrong).

What comes out in "finalyStore" is not the same as "original".

Please print the contents of both arrays and post to show us.
See the Arrays toString() method for easy display.

Take a look at this

System.arraycopy(Object Source, int SourceStartPos, Object Destination, int DestStartPos, int Length);

This copies data starting at Source[SourceStartPos] and goes to Source[SourceStartPos + Length] putting the values into Destination starting at DestStartPos and going to DestStartPos + Length. For you, it would be:

public class AClass
{
    private float[] original = new float[5];
    private float[] finalyStore = new float[5];

    public AClass()
    {
        //init orginal
        original[0] = .....
        finalyStore = testToCopy();
    }

    private float[] testToCopy()
    {
        float[] copyOfOriginal = new float[5];
        System.arraycopy(original, 0, copyOfOriginal, 0, 5);
        return copyOfOriginal;
    }
}

Your copy method looks OK to me too (ie should work).
But note the following does not do what you seem to think it does:

If I just copy the entire array it works fine:

copyOfOriginal = original;
    return copyOfOriginal;

Array variables (original, copyOfOriginal) do not contain arrays, they contain references to arrays. So copyOfOriginal = original; takes the reference value of original and sets copyOfOriginal to contain the same reference value.
At this point you still have only one array, and both original and copyOfOriginal contain identical references to that one array. At first sight this will look like you have successfully copied the array, but you haven't - eg change original[0] to a new value, and copyOfOriginal[0] will immediately show the same change because it's exactly the same array, not a copy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.