I have this list of objects that includes an array i (want) to use just as reference. So when i create a new object, fill it with an array in the list and start changing that new object i do not want my initial arrays for the object in my list to change.
I basically do this:
//Fill list with my reference objects;
Object newObject = new Object(); //So i do not change the previous newObject in the loop.
newObject = (find)ObjectFromList;
newObject.array = RotateArray(newObject.array);
If i fill another newObject with the same object from the list it already is rotated. I hope i have been clear enough. Below a shortened version of my code, still a bit messy too:
LoadRooms(); //Loads all the objects and arrays from a file into the list.
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
Room newRoom = new Room();
//Fill newroom with correct room type, rotate and build tilemap.
//Dead ends
if(!mazeMap[x][y].N && !mazeMap[x][y].E && mazeMap[x][y].S && !mazeMap[x][y].W)
{
newRoom = FindRoom(Room.RoomType.DeadEnd);
newRoom.room = TurnRoomCW(newRoom.room);
newRoom.room = TurnRoomCW(newRoom.room);
newRoom.room = TurnRoomCW(newRoom.room);
}
else if(!mazeMap[x][y].N && mazeMap[x][y].E && !mazeMap[x][y].S && !mazeMap[x][y].W)
{
newRoom = FindRoom(Room.RoomType.DeadEnd);
}
//Etc, etc then i build a map from the newRoom.room array
}
}
This is what TurnRoom() looks like:
private String[][] TurnRoomCW(String[][] room)
{
String[][] rotatedRoom = new String[room[0].length][room.length];
for (int y = 0; y < room[0].length;y++)
{
for (int x = 0;x < room.length;x++)
{
rotatedRoom[y][x] = room[7 - x][y];
}
}
return rotatedRoom;
}
and here is FindRoom
private Room FindRoom(Room.RoomType roomType)
{
Collections.shuffle(rooms, rand);
for (Room r : rooms)
{
if (r.roomType.equals(roomType))
return r;
}
return null;
}
When i want to turn something like a corner type room, say NE into the correct position all other rooms turn with it. So when i want to turn, say SW into position the NE will be position wrong again.
Your FindRoom method is returning a reference to the actual room.
'TurnRoomCW' returns a new object, but you then assign that new object back into the original room
So your problem is right here:
newRoom = FindRoom(Room.RoomType.DeadEnd); // 1) find a DeadEnd room
newRoom.room = TurnRoomCW(newRoom.room); // 2) create rotated room, assign it to the room from step 1)
If you want to work with a new Room object, you will need to create a new one somehow. For example, you might define a constructor for Room that returns a new object initialized from an existing one. For example,
/** copy constructor */
public Room(Room oldRoom) {
this(); // regular constructor
this.room = oldRoom.room.clone(); // new Room gets its own array!
this.roomType = oldRoom.roomType;
// … etc for any other member variables
}
Basically, Java objects are references (pointers if you prefer), so unless you make an explicit copy of an Array (or any other object), it will point to the same object.
If you want to avoid that, you have to do a clone first:
List myList = referenceList.clone();
This is generally a good habit anyway to avoid having your "internal" object being modified by the external world.
Related
I have a problem with my code and I do not find the mistake, must be something trivial.
// This list is filled with Objects of Matcher
ArrayList<Matcher > fullListForBundle = new ArrayList<>();
// making a new ArrayList
ArrayList<Matcher> bundlelist = new ArrayList<>();
// making a new object
Matcher currentBundle = new Matcher();
// Searching trough an Arraylist of Objects.
for (Matcher current : stockDataCompleteWithBundle)
{
// Get an Identifier
String han = current.getThirdColumn();
// Search through an other list to match identifier
for (int i = 0; i < fullListForBundle.size(); i++)
{
// If identifier matches then do:
if (fullListForBundle.get(i).getFifteenthColumn().equals(han))
{
// I want to get the right object and save it in currentBundle
currentBundle = fullListForBundle.get(i);
// !!! Here begins my problem !!!
// Then I want to change two Strings in that particular Object
currentBundle.setFirstColumn(current.getFirstColumn());
currentBundle.setThirteenthColumn(current.getSecondColumn());
// And add that object to a new Arraylist
bundlelist.add(currentBundle);
}
}
}
My Problem is: By setting the firstColumn and thirteenthColumn, the data in the fullListBundle.get(i) Object is changed and not the currentBundle Object. What am I missing?
When you do,
currentBundle = fullListForBundle.get(i);
Both currentBundle and fullListForBundle.get(i) referred to the same object in the heap. You should see the same results with both. If you just want the currentBundle to have your changes try,
currentBundle = fullListForBundle.get(i).clone();
EDIT : Object.clone() method has protected access, meaning it's visible to sub-classes and classes in the same package.
It's good to have a copy constructor for manually copying the object.
/**
Deep copy all the information from other to this
*/
public Matcher(Matcher other) {
this.id = other.id;
}
Read Why a copy constructor by Josh Bloch ?
This is because you are using the same object. you need to get a cloned object and do changes.
currentBundle = fullListForBundle.get(i).clone()
I'm trying to figure out how to get a undo function for a small maze game. First I worked out a way to do this by checking what the last direction was and just going back in the opposite direction. But this code was getting way too long since I also had to track back possible item pickups or hidden walls etc.
Background info on the code: I use a String[][] to store the maze as this was the easiest. I use an Arraylist<String[][]> to store all the strings.
After each step the player takes I save the String[][] array to the arraylist. When the player say undo I look at the second last String[][] in the arraylist and want to set the String[][] back to this. But the currentPos never seems to get updated. I'm not sure where the problem lies.
if (direction.equals("north")) {
if (currentPos[i - 1][j].equals("---")) {
continue;
} else {
currentPos[i][j] = " ";
currentPos[i - 2][j] = "P";
break;
}
}
if (direction.equals("undo")) {
currentPos = history.get(history.size()-2);
history.remove(history.size()-1);
break;
}
Without understanding the way you are setting history, I've made the assumption from your question that you are simply adding the current map to the history list. If you aren't careful, you will be simply adding the same Object, populating the history with multiply Object references to the current map state. This would have the effect you are observing with the state not changing, because you the history only contains a reference to the most recent map (Not storing any actual history).
To obtain the value from an Object, you typically need to clone the object (invoking the clone() method). However, cloning a 2-dimensional array is somewhat problematic. Invoking the clone() method on a 2-dimensional array "shallow" clones the object, essentially only cloning the first dimension while leaving the second as a reference to the same object (The reason for this is that the first 1-dimension of the array holds a reference to the second 1-dimension). Changing the value on a shallow copied object will change the value of the original and vice-versa, not what you want if you want to keep the Objects distinct.
To create two distinct objects, you will need to perform a "deep" clone, which can be easily implemented in a helper method. The below code illustrates the importance of ensuring you fully clone the object before storing it in the history list.
public static void main (String args[]) throws Exception {
ArrayList<String[][]> list = new ArrayList<>();
String[][] shallowClonedMap = new String[1][1];
String[][] deepClonedMap = new String[1][1];
shallowClonedMap[0][0] = "Old";
deepClonedMap[0][0] = "Old";
list.add(shallowClonedMap.clone());
list.add(deepClone(deepClonedMap));
shallowClonedMap[0][0] = "New";
deepClonedMap[0][0] = "New";
list.add(shallowClonedMap.clone());
list.add(deepClone(deepClonedMap));
for (String[][] item : list) {
System.out.print(item[0][0]);
}
}
public static String[][] deepClone(String[][] arry) {
if (arry == null) {
return null;
}
String[][] clone = new String[arry.length][];
for (int i = 0; i < arry.length; i++) {
clone[i] = arry[i].clone();
}
return clone;
}
The output for executing this code is : NewOldNewNew whereas the "intended" output is "OldOldNewNew". From this you can see the shallowClonedMap was updated to "New" even after being cloned and added to the list.
The code is :
package classes;
public class Test {
private static double mutationRate = 0.5;
public static void main(String[] args) {
Population pop = new Population();
pop.initialise();
Population po = new Population();
po.getIndividusList().add(pop.getFittest());
po.getIndividusList().add(mutate(pop.getIndividusList().get(1)));
}
private static Chromosom mutate(Chromosom l) { // changer les couples d'interventions des parcs)
// loop through genes
Chromosom ch = new Chromosom();
for (int i = 0; i < l.size(); i++)
ch.put(i, l.get(i));
for (int i = 0; i < ch.size(); i++) {
double alea = Math.random() * 13;
int moisIntervention1 = (int) alea;
Intervention interv1 = new Intervention(1, moisIntervention1);
ch.get(i).modInterventions(ch.get(i).intervention2(interv1));
}
return ch;
}
}
The problem is that I did not change the instance pop but when I change the other instance po, pop changes too.
java pass by value.
when you call this mutate(pop.getIndividusList().get(1))
you are sending pop's instance, so it will get change.
Supose pop.getIndividusList().get(1) return String varibale do this way
String var=pop.getIndividusList().get(1);
then call mutate(var)
I'm unsure about whether I understood the problem, but I think that you mean that when you alter the items in Population po, the items in Population pop mirror those changes.
That is, indeed, the expected behavior of your code: to populate po, you are adding items from pop - (pop.getFittest, pop.getList.get(1) ).
But the individuals are, I believe, instances of objects, so add/remove and similar operations work with references to the objects, and not with copies of them. Therefore, as you have 2 references to the same obj, any change is mirrored.
IF you want to create a copy, you should add to po a new object with the same state, either by creating a constructor that takes another instance as parameter, implementing a copy method, or something similar.
It should be something like this:
Population po = new Population();
Individual fittest = pop.getFittest();
Individual poCopy = new Individual();
//ADD CODE HERE TO COPY ALL THE FIELDS FROM fittest TO poCopy
//....
po.getIndividusList().add(poCopy);
I'm trying to generate new unique objects from an array of all possible objects to another array. The idea is that I have 3 classes that implement Region class and they have their own methods. These 3 classes are in my ArrayList<Region> arr. I pick a random class and add it to ArrayList<Region> ALL_REGIONS in a for loop. The problem is that the object that is added from arr is not unique, they are the same. This ca be told by their name. Every Region must have it's unique name and other settings but they don't. So this is the code I have so far:
public void generateRegions(){
ArrayList<Region> arr = Regions.getAllRegions();
Random rnd = new Random();
String ntype;
int regcounter = 5;
int b;
for(int i = 0; i < regcounter; i++){
ALL_REGIONS.add(arr.get(rnd.nextInt(arr.size())));
ntype = "n" + ALL_REGIONS.get(i).getType();
b = rnd.nextInt(Regions.getNtypeSize(ntype));
UI.print("b: " + b);
ALL_REGIONS.get(i).setName(Regions.getArrayName(ntype, b));
}
}
public static ArrayList<Region> getAllRegions(){
ArrayList<Region> arr = new ArrayList<Region>();
arr.add(new Highlands());
arr.add(new Cave());
arr.add(new Oasis());
return arr;
}
getArrayName returns a String name of the Region from an array and getNtypeSize returns an int, size of the array String[] that contatins all names which is not really important just now.
So.. how can I have every Cave, every Oasis unique/as a separate object?
**EDIT: ** Requested getArrayName() and getNtypeSize() methods are below:
public static String getArrayName(String ntype, int t) {
String ans = null;
if(ntype.equals("ncave")){
if(t<=ncaveSize)
ans = ncave[t];
}else if(ntype.equals("noasis")){
if(t<=noasisSize)
ans = noasis[t];
}else if(ntype.equals("nhighlands")){
if(t<=noasisSize)
ans = nhighlands[t];
}
//Can happen when t is bigger then ntype size or
// if ntype string is wrong
if(ans == null){
UI.printerr("getArrayNames: ans is empty/null");
}
UI.printerr(ans);
return ans;
}
public static int getNtypeSize(String ntype){
int ans = 0;
if(ntype.equals("ncave")){
ans = ncaveSize;
}else if(ntype.equals("noasis")){
ans = noasisSize;
}else if(ntype.equals("nhighlands")){
ans = nhighlandsSize;
}else
UI.printerr("getNtypeSize: returned 0 as an error");
return ans;
}
The issue is in this line:
ALL_REGIONS.add(arr.get(rnd.nextInt(arr.size())));
Here, you're not adding a new object to ALL_REGIONS. Rather, each time you're adding a reference to an object in 'arr'.
For example, each time rnd.nextInt(arr.size()) returns 2, you would add a reference to arr[2] to ALL_REGIONS. Thus, effectively, each entry in ALL_REGIONS refers to one of the objects in arr. (In this specific example, one of 3 objects you added in getAllRegions())
Effectively, this means that every Highlands object reference in ALL_REGIONS points to the same object => arr[0]
Similarly, every Cave reference in ALL_REGIONS points to arr[1] and every Oasis reference points to arr[2]
Something along this line should fix the issue:
Region reg = arr.get(rnd.nextInt(arr.size()))
ALL_REGIONS.add(reg.clone()); // this is just meant to be a sort of pseudo-code. Use a clone() method to create a new copy of the object and that copy to ALL_REGIONS.
If I got it right? You want to cast back to the type of the original object. It is plenty easy to do so, you will use some of the Java Polymorphism concepts.
You will use a function called InstanceOf like this
Region ob = arr[0];
if (ob instanceof Highlands)
Highlands newOb = (Highlands) ob;
So lets say I want to make a deep copy of an object, but using its contsructor. So I have:
public class PositionList {
private Position[] data = new Position[0];
private int size = 0;
public PositionList(PositionList other, boolean deepCopy) {
if (deepCopy==true){
size=other.getSize();
for (int i=0;i<data.length;i++)
data[i]=other.data[i];
}
else {
data=other.data;
size = other.size;
And so say I have this being called:
PositionList list = new PositionList();
PositionList acopy = new PositionList(list, true);
What I am doing, however, is incorrect, and Im not sure why..
The problem lies in your deep copy logic:
size=other.getSize();
for (int i=0;i<data.length;i++)
data[i]=other.data[i];
You are setting the size field (which is redundant with the data array) but are not assigning a new array to the data field, which is presumably the whole point of your "deep" copy. You should initialize data to the other's size (or other.data.length):
data = new Position[other.data.length];
for (int i=0;i<data.length;i++)
data[i]=other.data[i];
(And get rid of size all together)