Previous ArrayList elements gets overwritten by new element [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
problem with assigning an array to other array in java
4 answers
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
EDIT 1: Tried doing the solution found in the linked question but it did not solved my problem. Now I don't really have any idea in solving this. Also I updated the code snippet.
EDIT 2: Find another issue that contributed to my problem and I managed to fix it.
I have a State class with the following structure:
public class State {
int puzzle;
int heuristic;
...
}
There is a part of the program where I edit a row and add it to an arraylist, the problem is whenever I'm editing a new State's puzzle it would also affect previously added States in the ArrayList.
Here is the snippet where I add new State to an ArrayList:
public int copyPuzzle(int puzzle) {
int res = new int[puzzle.length][puzzle[0].length];
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle.length; j++) {
res[i][j] = puzzle[i][j];
}
}
return res;
}
public ArrayList<State> generate() {
ArrayList<State> successors = new ArrayList<State>();
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
for(int k = 0; k < puzzle[i].length; k++) {
if(puzzle[i][j] != puzzle[i][k] && puzzle[i][j] <= 0 && puzzle[i][k] <= 0) {
int newPuzzle = copyPuzzle(puzzle);
int temp = newPuzzle[i][j];
newPuzzle[i][j] = newPuzzle[i][k];
newPuzzle[i][k] = temp;
State state = new State(newPuzzle);
successors.add(state);
}
}
}
}
return successors;
}
java arraylist
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
problem with assigning an array to other array in java
4 answers
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
EDIT 1: Tried doing the solution found in the linked question but it did not solved my problem. Now I don't really have any idea in solving this. Also I updated the code snippet.
EDIT 2: Find another issue that contributed to my problem and I managed to fix it.
I have a State class with the following structure:
public class State {
int puzzle;
int heuristic;
...
}
There is a part of the program where I edit a row and add it to an arraylist, the problem is whenever I'm editing a new State's puzzle it would also affect previously added States in the ArrayList.
Here is the snippet where I add new State to an ArrayList:
public int copyPuzzle(int puzzle) {
int res = new int[puzzle.length][puzzle[0].length];
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle.length; j++) {
res[i][j] = puzzle[i][j];
}
}
return res;
}
public ArrayList<State> generate() {
ArrayList<State> successors = new ArrayList<State>();
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
for(int k = 0; k < puzzle[i].length; k++) {
if(puzzle[i][j] != puzzle[i][k] && puzzle[i][j] <= 0 && puzzle[i][k] <= 0) {
int newPuzzle = copyPuzzle(puzzle);
int temp = newPuzzle[i][j];
newPuzzle[i][j] = newPuzzle[i][k];
newPuzzle[i][k] = temp;
State state = new State(newPuzzle);
successors.add(state);
}
}
}
}
return successors;
}
java arraylist
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
problem with assigning an array to other array in java
4 answers
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
EDIT 1: Tried doing the solution found in the linked question but it did not solved my problem. Now I don't really have any idea in solving this. Also I updated the code snippet.
EDIT 2: Find another issue that contributed to my problem and I managed to fix it.
I have a State class with the following structure:
public class State {
int puzzle;
int heuristic;
...
}
There is a part of the program where I edit a row and add it to an arraylist, the problem is whenever I'm editing a new State's puzzle it would also affect previously added States in the ArrayList.
Here is the snippet where I add new State to an ArrayList:
public int copyPuzzle(int puzzle) {
int res = new int[puzzle.length][puzzle[0].length];
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle.length; j++) {
res[i][j] = puzzle[i][j];
}
}
return res;
}
public ArrayList<State> generate() {
ArrayList<State> successors = new ArrayList<State>();
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
for(int k = 0; k < puzzle[i].length; k++) {
if(puzzle[i][j] != puzzle[i][k] && puzzle[i][j] <= 0 && puzzle[i][k] <= 0) {
int newPuzzle = copyPuzzle(puzzle);
int temp = newPuzzle[i][j];
newPuzzle[i][j] = newPuzzle[i][k];
newPuzzle[i][k] = temp;
State state = new State(newPuzzle);
successors.add(state);
}
}
}
}
return successors;
}
java arraylist
This question already has an answer here:
problem with assigning an array to other array in java
4 answers
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
EDIT 1: Tried doing the solution found in the linked question but it did not solved my problem. Now I don't really have any idea in solving this. Also I updated the code snippet.
EDIT 2: Find another issue that contributed to my problem and I managed to fix it.
I have a State class with the following structure:
public class State {
int puzzle;
int heuristic;
...
}
There is a part of the program where I edit a row and add it to an arraylist, the problem is whenever I'm editing a new State's puzzle it would also affect previously added States in the ArrayList.
Here is the snippet where I add new State to an ArrayList:
public int copyPuzzle(int puzzle) {
int res = new int[puzzle.length][puzzle[0].length];
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle.length; j++) {
res[i][j] = puzzle[i][j];
}
}
return res;
}
public ArrayList<State> generate() {
ArrayList<State> successors = new ArrayList<State>();
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
for(int k = 0; k < puzzle[i].length; k++) {
if(puzzle[i][j] != puzzle[i][k] && puzzle[i][j] <= 0 && puzzle[i][k] <= 0) {
int newPuzzle = copyPuzzle(puzzle);
int temp = newPuzzle[i][j];
newPuzzle[i][j] = newPuzzle[i][k];
newPuzzle[i][k] = temp;
State state = new State(newPuzzle);
successors.add(state);
}
}
}
}
return successors;
}
This question already has an answer here:
problem with assigning an array to other array in java
4 answers
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
java arraylist
java arraylist
edited Nov 19 at 20:36
asked Nov 19 at 20:04
Helquin
10510
10510
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes