1

I'm trying to convert an array to ArrayList so I can use the shuffle method, then convert the ArrayList to array, but I get in error and I don't know what is the problem

public class Main extends Application {
    // Declaring Some Objects
    int i;
    GridPane gridPane;
    List<Image> imgs;
    Image[] imgs2 = new Image[8];
    ArrayList<Button> btns = new ArrayList<>();
    Image[] imageCopy;

    public void arrOfImgs() {
        // Creating An Array List That Contains All Images
        for (i = 0; i < 8; i++) {
            imgs2[i] = new Image("Pictures/image" + i + ".jpg", 80, 80, true, true);
        }

        imageCopy = new Image[imgs2.length * 2];

        System.arraycopy(imgs2, 0, imageCopy, 0, imgs2.length);
        System.arraycopy(imgs2, 0, imageCopy, imgs2.length, imgs2.length);

        // convert from array to arraylist
        imgs = Arrays.asList(imageCopy);
        Collections.shuffle(imgs);

        imageCopy = (Image[]) imgs.toArray();

    }
}

this is the error

Caused by: java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljavafx.scene.image.Image; ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; [Ljavafx.scene.image.Image; is in module javafx.graphics of loader 'app')
    at sample.Main.arrOfImgs(Main.java:36)
    at sample.Main.start(Main.java:66)
Slaw
  • 37,820
  • 8
  • 53
  • 80
Saeedlafi
  • 11
  • 2

0 Answers0