Data is covered by latter data
Hi everyone
I want to add some data to an ArrayList array, but the latter data just covers the former one, while the size of the array is growing (I learned Java for 4 months by myself and did not take any data structure courses in my college...)
here is the code (they are in the same class):
ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>();
ArrayList<ArrayList<Double>> calcOutput(ArrayList<ArrayList<Double>> input) throws Exception {
ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
//calculation;
return out;
}
void allfire(ArrayList<ArrayList<ArrayList<Double>>> tch) throws Exception {
for (int p = 0; p < tch.size(); p++) {
all.add(calcOutput(tch.get(p)));
}
it happens that for p=1 or 2 or else, data in all is always changed to the last data added into all
how does this happen and how to solve it?
Thanks very much
Re: Data is covered by latter data
sounds like your are recreating your ArrayList<....<Double> >> somewhere down the line. can you post more of the code? Do you need a three dimensional ArrayList of ArrayList?
Re: Data is covered by latter data
Are you sure you need such monsters:
ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>(); ? :D
Re: Data is covered by latter data
My codes is boring in that I have no idea about data structure and object-oriented idea and made it a mess
public class Network {
ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>();
double[] weight;
ArrayList<ArrayList<Double>> calcOutput(ArrayList<ArrayList<Double>> input) throws Exception {
ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
//calculate with parameter weight[] and add the data into out
//weight[] will be changed in allfire method
return out;
}//end of calcOutput method
void allfire(ArrayList<ArrayList<ArrayList<Double>>> tch) throws Exception {
for (int pattern = 0; pattern < tch.size(); pattern++) {
boolean test = true;
while(test){// loop until the result(out) is perfect
// calc out
ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
out = calcOutput(tch.get(pattern));
//if the result(out) is not perfect, change parameter(weight[]) for a better result(a better out)
}//end of loop while
}//end of loop for
//weight[] is good enough to make perfect out for all patterns
//calculate out with this weight[] and add the data into all
all.clear();
for (int p = 0; p < tch.size(); p++) {
all.add(calcOutput(tch.get(p)));
}
}//end of allfire method
}//end of class
Re: Data is covered by latter data
for example
for p=0 add [[3.0],[3.2,3.3]] into all
for p=1 add [[4.2,5.3]] into all
then data in all turns out to be[[[4.2,5.3]],[[4.2,5.3]]] which should be [[[3.0],[3.2,3.3]],[[4.2,5.3]]]
seems that for each calcOutput(tch.get(p)),it is the data in the memory changed so that the former data is also changed
Re: Data is covered by latter data
Do you have code to test your program? I don't see a main() method or any calls to your methods.