Debugged my code and all variables show up as expected:
class1 {
callingMethod() {
Double[] entry = new Double[size];
while(condition) {
for (i over entry) {
entry[i] = Double.parseDouble(token.nextToken());
}
DataSet.addInput(entry);
}
}
}
DataSet {
private ArrayList<Double[]> inputsList;
public DataSet (...) {
list = new <Double[]>ArrayList();
}
public void addInput(Double[] anInputVector) {
inputsList.add(anInputVector);
}
}
'theEntry' is properly populated array of Doubles with each new entry. When each new entry is added to 'list', the first list entry is modified with new double values, and a null entry is added. result is 1st entry is an array of doubles (last added), followed by a long list of nulls. Any ideas?
It seems a case of sharing an array object. An example:
List<double[]> list = new ArrayList<>();
double[] a = new double[] { 1.0, 2.0 };
double[] b = new double[] { 3.0, 4.0 };
// Three new's: three objects.
list.add(a);
list.add(a);
list.add(b);
a[0] = 5.0;
list.get(0)[1] = 6.0;
// list: [ [5.0, 6.0]==a, [5.0, 6.0]==a [3.0, 4.0]==b ]
One solution:
public void addEntry(Double[] theEntry) {
list.add(Arrays.copyOf(theEntry, theEntry.length));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.