Array List 值即使未更改也会更改
Array List value changes even when it is not changed
在方法updateMasterGrid()中,
1st 我正在显示来自 c.getBall 的值。源是 quadCellGrid arraylist
2nd 我将 quadCellGrid 的值分配给 mastergrid
3rd 我正在显示 c.getBall。源是 quadCellGrid arraylist
将 quadCellGrid 中的值分配给 mastergrid 之后。我可以看到 quadCellGrid 中值的变化。请检查 logcat 输出。
你能告诉我如何解决这个问题吗?
public class BurstBalls {
private boolean Q1Match,Q2Match,Q3Match,Q4Match;
private Texture RED_BALL;
private Texture BURST_STAR;
private List<CellGrid> masterGrid;
private List<CellGrid> quadCellGrid = new ArrayList<CellGrid>();
private List<CellGrid> burstCellGrid = new ArrayList<CellGrid>();
private SpriteBatch batch;
private float scaleXY = 0.1f;
private int Q1Moves[][] = { { 0, 0 },{ -1, 0 }, { 0, 1 }, { -1, 1 } };
private int Q2Moves[][] = { { 0, 0 },{ 1, 0 }, { 0, 1 }, { 1, 1 } };
private int Q3Moves[][] = { { 0, 0 },{ 1, 0 }, { 0,-1 }, { 1,-1 } };
private int Q4Moves[][] = { { 0, 0 },{ -1, 0 }, { 0,-1 }, { -1,-1 } };
public BurstBalls( ) {
setGameTextures();
}
public void draw(SpriteBatch sb){
batch=sb;
if(!burstCellGrid.isEmpty()){
showImageZoom1(BURST_STAR, burstCellGrid.get(0).getColCoordinate()/2, burstCellGrid.get(0).getRowCoordinate()/2);
}
}
private void showImageZoom1(Texture t, int x, int y) {
scaleXY = scaleXY + 0.05f;
if (scaleXY >= 1.0){
//burstCellGrid.clear();
scaleXY = 1.0f;
}
Sprite s = new Sprite(t);
s.setPosition(x, y);
s.setScale(scaleXY);
s.draw(batch);
}
public List<CellGrid> getMatchBallCells(int row, int col, Ball b, List<CellGrid> mGrid){
this.masterGrid=mGrid;
Q1Match=false;
Q2Match=false;
Q3Match=false;
Q4Match=false;
quadCellGrid.clear();
burstCellGrid.clear();
if(row<(MainGame.ROW-1) && col >0){
Q1Match = checkCells(b, row,col, Q1Moves);
System.out.println("Q1Match : " + Q1Match);
}
if(row<(MainGame.ROW-1) && col < (MainGame.COL-1)){
Q2Match = checkCells(b,row,col, Q2Moves);
System.out.println("Q2Match : " + Q2Match);
}
if(row>0 && col< (MainGame.COL-1)){
Q3Match = checkCells(b, row,col, Q3Moves);
System.out.println("Q3Match : " + Q3Match);
}
if(row>0 && col > 0){
Q4Match = checkCells(b, row,col, Q4Moves);
System.out.println("Q4Match : " + Q4Match);
}
if(Q1Match || Q2Match || Q3Match || Q4Match){
updateMasterGrid();
}
for (CellGrid c : burstCellGrid) {
if(c.getBall()!=null){
System.out.println("!Burst Cells - (c.getRow(),c.getCol) - " + "(" + c.getRow() +","+c.getCol() +")");
}
}
return masterGrid;
}
private void updateMasterGrid() {
for(CellGrid c: quadCellGrid){
System.out.println(" Before quadCellGrid.ball " + c.getBall());
masterGrid.get(masterGrid.indexOf(c)).setBall(null);
System.out.println(" After quadCellGrid.ball " + c.getBall());
}
}
private boolean checkCells(Ball actionBall,int row,int col,int moves[][]) {
boolean firstCell = false,secondCell = false,thirdCell = false,fourthCell = false;
CellGrid cellGrid=checkIfBallThere(row+moves[0][1],col+moves[0][0]);
firstCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[1][1],col+moves[1][0]);
secondCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[2][1],col+moves[2][0]);
thirdCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[3][1],col+moves[3][0]);
fourthCell = checkBall(cellGrid,actionBall);
if(firstCell && secondCell && thirdCell && fourthCell){
return true;
}
return false;
}
private boolean checkBall(CellGrid c, Ball actionBall) {
if(c!=null && c.getBall().getTexture().equals(actionBall.getTexture())){
if (!quadCellGrid.contains(c)){
quadCellGrid.add(c);
}
return true;
}
return false;
}
public CellGrid checkIfBallThere(int cellRow, int cellCol ) {
for (CellGrid c : masterGrid) {
if (c.getRow() == cellRow && c.getCol() == cellCol
&& c.getBall() != null) {
return c;
}
}
return null;
}
private void setGameTextures() {
RED_BALL = Texturemanager.RED_BALL;
RED_BALL.setFilter(TextureFilter.Linear, TextureFilter.Linear);
BURST_STAR = Texturemanager.BURST_STAR;
BURST_STAR.setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
}
Logcat
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41414ed8
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41415670
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@414155e0
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41415628
After quadCellGrid.ball null
我不知道你是如何填充 mastergrid 和 quadcellgrid 的,但这条线对我来说似乎是罪魁祸首。
masterGrid.get(masterGrid.indexOf(c)).setBall(null);
您可能在 MastCellGrid 和 Quadcellgrid 中存储了相同的 Cellgrid 对象实例,因此在首先打印它时,您将其设置为 null 然后再次打印它,打印为 null。
检查或post你的围栏人口逻辑。
List 是 references 的列表。如果您从一个列表中获取引用并将其添加到另一个列表,则两个列表都引用了相同的对象。
如果您获得对此类对象的引用并对其进行修改(设置字段),更改将反映在两个 列表的相关元素中。
在方法updateMasterGrid()中, 1st 我正在显示来自 c.getBall 的值。源是 quadCellGrid arraylist
2nd 我将 quadCellGrid 的值分配给 mastergrid
3rd 我正在显示 c.getBall。源是 quadCellGrid arraylist
将 quadCellGrid 中的值分配给 mastergrid 之后。我可以看到 quadCellGrid 中值的变化。请检查 logcat 输出。
你能告诉我如何解决这个问题吗?
public class BurstBalls {
private boolean Q1Match,Q2Match,Q3Match,Q4Match;
private Texture RED_BALL;
private Texture BURST_STAR;
private List<CellGrid> masterGrid;
private List<CellGrid> quadCellGrid = new ArrayList<CellGrid>();
private List<CellGrid> burstCellGrid = new ArrayList<CellGrid>();
private SpriteBatch batch;
private float scaleXY = 0.1f;
private int Q1Moves[][] = { { 0, 0 },{ -1, 0 }, { 0, 1 }, { -1, 1 } };
private int Q2Moves[][] = { { 0, 0 },{ 1, 0 }, { 0, 1 }, { 1, 1 } };
private int Q3Moves[][] = { { 0, 0 },{ 1, 0 }, { 0,-1 }, { 1,-1 } };
private int Q4Moves[][] = { { 0, 0 },{ -1, 0 }, { 0,-1 }, { -1,-1 } };
public BurstBalls( ) {
setGameTextures();
}
public void draw(SpriteBatch sb){
batch=sb;
if(!burstCellGrid.isEmpty()){
showImageZoom1(BURST_STAR, burstCellGrid.get(0).getColCoordinate()/2, burstCellGrid.get(0).getRowCoordinate()/2);
}
}
private void showImageZoom1(Texture t, int x, int y) {
scaleXY = scaleXY + 0.05f;
if (scaleXY >= 1.0){
//burstCellGrid.clear();
scaleXY = 1.0f;
}
Sprite s = new Sprite(t);
s.setPosition(x, y);
s.setScale(scaleXY);
s.draw(batch);
}
public List<CellGrid> getMatchBallCells(int row, int col, Ball b, List<CellGrid> mGrid){
this.masterGrid=mGrid;
Q1Match=false;
Q2Match=false;
Q3Match=false;
Q4Match=false;
quadCellGrid.clear();
burstCellGrid.clear();
if(row<(MainGame.ROW-1) && col >0){
Q1Match = checkCells(b, row,col, Q1Moves);
System.out.println("Q1Match : " + Q1Match);
}
if(row<(MainGame.ROW-1) && col < (MainGame.COL-1)){
Q2Match = checkCells(b,row,col, Q2Moves);
System.out.println("Q2Match : " + Q2Match);
}
if(row>0 && col< (MainGame.COL-1)){
Q3Match = checkCells(b, row,col, Q3Moves);
System.out.println("Q3Match : " + Q3Match);
}
if(row>0 && col > 0){
Q4Match = checkCells(b, row,col, Q4Moves);
System.out.println("Q4Match : " + Q4Match);
}
if(Q1Match || Q2Match || Q3Match || Q4Match){
updateMasterGrid();
}
for (CellGrid c : burstCellGrid) {
if(c.getBall()!=null){
System.out.println("!Burst Cells - (c.getRow(),c.getCol) - " + "(" + c.getRow() +","+c.getCol() +")");
}
}
return masterGrid;
}
private void updateMasterGrid() {
for(CellGrid c: quadCellGrid){
System.out.println(" Before quadCellGrid.ball " + c.getBall());
masterGrid.get(masterGrid.indexOf(c)).setBall(null);
System.out.println(" After quadCellGrid.ball " + c.getBall());
}
}
private boolean checkCells(Ball actionBall,int row,int col,int moves[][]) {
boolean firstCell = false,secondCell = false,thirdCell = false,fourthCell = false;
CellGrid cellGrid=checkIfBallThere(row+moves[0][1],col+moves[0][0]);
firstCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[1][1],col+moves[1][0]);
secondCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[2][1],col+moves[2][0]);
thirdCell = checkBall(cellGrid,actionBall);
cellGrid=checkIfBallThere(row+moves[3][1],col+moves[3][0]);
fourthCell = checkBall(cellGrid,actionBall);
if(firstCell && secondCell && thirdCell && fourthCell){
return true;
}
return false;
}
private boolean checkBall(CellGrid c, Ball actionBall) {
if(c!=null && c.getBall().getTexture().equals(actionBall.getTexture())){
if (!quadCellGrid.contains(c)){
quadCellGrid.add(c);
}
return true;
}
return false;
}
public CellGrid checkIfBallThere(int cellRow, int cellCol ) {
for (CellGrid c : masterGrid) {
if (c.getRow() == cellRow && c.getCol() == cellCol
&& c.getBall() != null) {
return c;
}
}
return null;
}
private void setGameTextures() {
RED_BALL = Texturemanager.RED_BALL;
RED_BALL.setFilter(TextureFilter.Linear, TextureFilter.Linear);
BURST_STAR = Texturemanager.BURST_STAR;
BURST_STAR.setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
}
Logcat
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41414ed8
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41415670
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@414155e0
After quadCellGrid.ball null
Before quadCellGrid.ball com.puzzle.game.ballpool.Ball@41415628
After quadCellGrid.ball null
我不知道你是如何填充 mastergrid 和 quadcellgrid 的,但这条线对我来说似乎是罪魁祸首。
masterGrid.get(masterGrid.indexOf(c)).setBall(null);
您可能在 MastCellGrid 和 Quadcellgrid 中存储了相同的 Cellgrid 对象实例,因此在首先打印它时,您将其设置为 null 然后再次打印它,打印为 null。
检查或post你的围栏人口逻辑。
List 是 references 的列表。如果您从一个列表中获取引用并将其添加到另一个列表,则两个列表都引用了相同的对象。
如果您获得对此类对象的引用并对其进行修改(设置字段),更改将反映在两个 列表的相关元素中。