0

Last objected added to the ArrayList overrides all elements in the ArrayList and I can't figure it out why.. even after reading all problem related topics on web.

I create new object each iteration? and all fields (move, board, color) are protected (I can't change that since I'm using a chessboard framework).

I haven't found the solution on web because my fields ain't static and I also create a new object after each iteration.

public String solve(String fen) {
    Chessboard cb = Chessboard.getChessboardFromFEN(fen);
    return solve(cb);
}

private String solve(Chessboard cb){
 ArrayList<Chessboard> prioritetna = new ArrayList<>();
 for(int i = 0; i < 10; i++){
  cb.makeMove(cb.getMoves().get(i));
  prioritetna.add(new Chessboard(cb.getBoard(), cb.getColor(), 
  cb.getMovesLeft()));
  cb.reverseMove(cb.getMoves().get(i));
 }
 return "";
}

And I use this framework that is made of three classes for Chessboard implementation: 1. Chessboard: https://pastebin.com/CqgHLCXM 2. Move: https://pastebin.com/g2wr16TP 3. Rules: https://pastebin.com/HEznyECY

I've also rewrote code with no framework dependencies and it works.. so i guess this is a framework problem.

Framework is also available as a JAR: https://ufile.io/c7n3o

public static void main(String[] args) {

    ArrayList<Object> list = new ArrayList<>();
    Random rand = new Random();

    for(int i = 0; i < 5; i++){
        list.add(new Object(rand.nextInt(50) + 1, rand.nextInt(50) + 1));
    }

    for(int i = 0; i < 5; i++){
        System.out.println(list.get(i).getOne());
    }

  }


public class Object {
  protected int one;
  protected int two;
public Object(int one, int two){
    this.one = one;
    this.two = two;
}

public int getOne() {
    return one;
}

public void setOne(int one) {
    this.one = one;
}

public int getTwo() {
    return two;
}

public void setTwo(int two) {
    this.two = two;
}

}

randomdude
  • 15
  • 3
  • Please provide a [mcve]. – shmosel Apr 13 '18 at 20:41
  • I provided JAR of a framework and all the minimal code needed to get a better understanding of my problem. – randomdude Apr 13 '18 at 20:56
  • 1
    No, no. That's not minimal at all. Try to boil down the problem to a small snippet that can be pasted into the question. – shmosel Apr 13 '18 at 20:58
  • [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Click this comment to find out how to provide what we need to help you.](https://stackoverflow.com/help/mcve) –  Apr 13 '18 at 20:58
  • I've tried rewriting it with newly created class and similar implementation and it worked.. so I guess this is a framework based problem.. how can I boil down that code? – randomdude Apr 13 '18 at 21:29
  • I've also pasted code that works. – randomdude Apr 13 '18 at 21:30
  • Try stripping down the broken code until it starts working. – shmosel Apr 13 '18 at 21:33
  • I cant strip solve() function? I think it's as stripped as possible – randomdude Apr 13 '18 at 21:35

0 Answers0