0

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;
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Mack
  • 45
  • 4
  • Avoid clone as much as possible. Use only when you have no other option. :http://www.javapractices.com/topic/TopicAction.do?Id=71 – Suresh Atta Oct 07 '13 at 09:00

5 Answers5

3
 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)
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
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.

John
  • 576
  • 1
  • 6
  • 14
0

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;
}
sol4me
  • 15,233
  • 5
  • 34
  • 34
0

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

-1

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.

Simon Mokhele
  • 3,913
  • 3
  • 21
  • 22