Search code examples
javasortingobjectarraylistmember-variables

Shuffle only one variable of objects in arraylist


I have an arraylist filled with objects of a class. These objects have variables like x,y and a imagePath. I only want to shuffle the imagepath variables in this arraylist between the objects. So x- and y variables of the objects should stay the same. How can I do this?

I know I can use collections.shuffle() to shuffle the entire arraylist. But this doesn't solve my problem.


Solution

  • Use one ArrayList for the objects and another for the images, shuffle the one with the images, and then make each object refer to the image on its same relative position.