Question

I am trying to reverse a Bitmap array, without modifying the source array. But problem is the source array is also getting reversed. Am I doing something wrong or am I suppose to do it some other way? Thanks for help.

private GalleryAdapter galleryAdapter;
private ReverseGalleryAdapter rGalleryAdapter;  

public void onCreate(Bundle savedInstanceState) {
    galleryAdapter = new GalleryAdapter(this, imageThumbnails, imagePaths,
            videoThumbnails, videoPaths);

    imagegrid.setAdapter(galleryAdapter);

    rGalleryAdapter = new ReverseGalleryAdapter(this, reverseBArray(videoThumbnails), reverseSArray(videoPaths),
            reverseBArray(imageThumbnails), reverseSArray(imagePaths));

    imagegrid2.setAdapter(rGalleryAdapter);
}

private Bitmap[] reverseBArray(Bitmap[] v){
    Bitmap[] bTemp;
    bTemp = v;
    int len = bTemp.length;
    Bitmap temp;
    for (int i = 0; i < len/2; i++)
    {
        temp = bTemp[i];
        bTemp[i] = bTemp[len-1 - i];
        bTemp[len-1 - i] = temp;
    }
    return bTemp;
}

private String[] reverseSArray(String[] s){
    String[] sTemp;
    sTemp =s;
    int len = sTemp.length;
    String temp;
    for (int i = 0; i < len/2; i++)
    {
        temp = sTemp[i];
        sTemp[i] = sTemp[len-1 - i];
        sTemp[len-1 - i] = temp;
    }
    return sTemp;
}
Était-ce utile?

La solution 2

With bTemp = v; you are referring to the same Object.

You can use in your case the clone() method to make a copy of the array:

bTemp = v.clone();

Then you can freely modify it and return it.

Autres conseils

 Bitmap[] bTemp;
 bTemp = v;

When you are doing this , defenitley both referencing to the same array, because you are not creating a new array there.

same happens here too

  String[] sTemp;
    sTemp =s;

Possible soution :Make copy of array Java (Suggesting to choose after reading possible ways).

My suggestion from Official docs:(See copying array section)

The System class has an arraycopy() method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src, int srcPos,
                             Object dest, int destPos, int length)

Remember array are object and in method reverseArray, you are reassiging the reference to temp, so now you have 2 references (v , and temp) to same object . Make the appropriate changes mentioned below

private Bitmap[] reverseBArray(Bitmap[] v){
    Bitmap[] bTemp;
    bTemp = v; // CHANGE IT to bTemp = new Bitmap[v.length]
    int len = bTemp.length;
    Bitmap temp;
    for (int i = 0; i < len/2; i++)
    {
        temp = bTemp[i];
        bTemp[i] = bTemp[len-1 - i];
        bTemp[len-1 - i] = temp;
    }
    return bTemp;
}

I think you want to keep both objects right? If so I think you can use clone() method and pass that cloned object to the method without modifying the original object

Java doesn't have pass objects by value. You pass the array object reference to you method thus the array itself. Best way would be to create a copy of the array and pass the copy of the array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top