java - Copying Array from ArrayList Element -
i'm building java based game in swing, grid of jbuttons
i have object called cell, custom jbutton additional parameters storing objects. game grid represented cell[][]
i have arraylist of type cell[][] allow me store state of gamegrid after each move. if want undo move, need copy last element of arraylist game grid allow displayed on ui.
my gamegrid panelholder , arraylist moveholder.
so far i've tried collections.copy(panelholder, moveholder.get(moveholder.size())); not compile due "arguments not being applicable type cell[][]"
i've tried system.arraycopy(moveholder.get(moveholder.size()-1), 0, panelholder, 0, panelholder.length);, throws , out of bounds exception. thought due moveholder.size()-1, moveholder.size() has same problem.
i've found numerous questions on stackoverflow , others both show these 2 ways of doing it, can't seem work. there more obvious i'm missing? full class method below:
public class undoclass implements movecommand{ public arraylist<cell[][]> moveholder = new arraylist<cell[][]>(); public cell[][] execute(cell[][] panelholder) { if (moveholder.size() > 0){ collections.copy(panelholder, moveholder.get(moveholder.size())); if (moveholder.size() > 0){ moveholder.remove(moveholder.size()); } } system.out.println("move undone. undos available:" + moveholder.size()); return panelholder; } public void addmove(cell[][] panelholder){ moveholder.add(panelholder); } public arraylist<cell[][]> getmoves(){ return moveholder; } } cell class
public class cell extends jbutton { int co_x = 0; int co_y = 0; arraylist<players> current = new arraylist <players>(); }
if (moveholder.size() > 0) { (int = 0; < panelholder.length; i++) { panelholder[i] = moveholder.get(moveholder.size()-1)[i].clone(); } moveholder.remove(moveholder.size()-1); } try this. need make copies of each internal array when copying 2d arrays.
Comments
Post a Comment