-2

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user7432713
  • 197
  • 3
  • 17
  • 3
    1. Put the `imagePath` values into a list. 2. Shuffle the list. 3. Put the `imagePath` values back into the fields of your objects. – khelwood Jun 09 '17 at 13:54
  • I think that this is bad design, when you need such operations. – xenteros Jun 09 '17 at 13:55
  • why dont you iterate your arraylist, extract all the imagePath to an array, shuffle it, then iterate the arraylist again and write then back – Gabri T Jun 09 '17 at 13:56

1 Answers1

0

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.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • Or kind of a relational link, the instance have an index ( 0 <= index < pathList.size). This will let you use an instance outside of the list as it will not need to know his position in the object list (not clear maybe..) – AxelH Jun 09 '17 at 13:57