say I have a class A which is as follows
public class A {
protected static float[] floatArray = null;
protected static Map<Integer, float[]> history = new HashMap<Integer,float[]>();
protected static Integer historyCount = 0;
public void runEverySecond(Populator objPopulator) {
floatArray = objPopulator.getValues();
history.put(historyCount, floatArray);
historyCount++;
}
}
and Class B looks as follows
public class B {
private A objA;
protected final static Populator objPopulator = new Populator();
public void run(Integer numOfTime) {
for(int i = 0; i < numOfTime; i++)
objA.runEverySecond(objPopulator);
}
}
and Class Populator looks as follows
public class Populator {
protected float[] randomValues = new float[2];
public float[] getValues() {
randomValues[0] = //some new random float value generated for every call
randomValues[1] = //some new random float value generated for every call
return randomValues;
}
}
and class containing main looks as follows
public class MainClass {
public static void main() {
final B objB = new B();
objB.run(10);
}
}
Here is the problem I am facing, the Map history contains the same value for every entry in the map. I want the Map history to store all the values generated by objPopulator.getValues() method. How do I do it?
Some help would be really appreciable.
Thanks in Advance 🙂
The Actual code (with irrelevant code removed ) represented by class A
public class MySuperAgent implements Agent {
protected static float[] marioFloatPos = null;
protected static Map<Integer, float[]> levelRecord = new HashMap<Integer, float[]>();
protected static Integer mapCount = 0;
/* protected static int testCount = 0;
protected static float[] testx = new float[2];
protected static float[] testy = new float[2];*/
@Override
public void integrateObservation(Environment environment) {
marioFloatPos = environment.getMarioFloatPos();
levelRecord.put(mapCount, marioFloatPos);
mapCount++;
/*if(testCount < 2){
testx[testCount] = marioFloatPos[0];
testy[testCount] = marioFloatPos[1];
testCount++;
} else {
testCount = 0;
}*/
}
}
class C represent environment object
integrateObservation method is called from class similar to class B
if I use the code within the comment block then I am able to record only 2 past values of x and y of Mario. I need a way to store all the values of x and y of Mario 🙂
There’s only one
floatArrayand you keep reassigning it.This code was almost impossible to follow in a reasonable way–what a mess.