I\'m trying to create a 2d ArrayList(can\'t use Arrays). It will be an Arraylist
ID: 3747913 • Letter: I
Question
I'm trying to create a 2d ArrayList(can't use Arrays). It will be an Arraylist of Arraylist
ArrayList<ArrayList<Double>> correlationMatrix = new ArrayList<ArrayList<Double>>();
ArrayList<ArrayList<Double>> newcorrelationMatrix = new ArrayList<ArrayList<Double>>();
ArrayList<Double> lastCorrelationMatrix = new ArrayList<Double>();
for (int i=0;i<correlationMatrix.size();i++)
{
for (int j=0;j<correlationMatrix.size();j++)
{
lastCorrelationMatrix.add(findCorrelation(correlationMatrix.get(i),correlationMatrix.get(j)));
}
newcorrelationMatrix.add(lastCorrelationMatrix);
lastCorrelationMatrix.clear();
}
correlationMatrix is filled with 5 ArrayList<Double> and the findCorrelation method takes 2 ArrayList<Double> as arguments.
I'm trying to fill newcorrelationMatrix in a way that each row is each iteration of lastCorrelationMatrix. My thought process was that for example for i=0 fill lastCorrelationMatrix, and add that Arraylist to newcorrelationMatrix and then clear the content of lastCorrelationMatrix and go to i=1 and add the new lastCorrelationMatrix to newcorrelationMatrix, and so on. But for some reason is just printinig the i=4 value repeated 5 times. The method findCorrelation works, the purpose of me creating newcorrelationMatrix is that lastCorrelationMatrix after the method call fills it 20 elements and I need them to be output in a 5x4 matrix format. What am I doing wrong in this code, thanks.
Explanation / Answer
After debug found issue with add and clearing the content of lastCorrelation matrix
issuw with only this line
newcorrelationMatrix.add(lastCorrelationMatrix);
lastCorrelationMatrix.clear();
after adding the arraylist to newcorrelationMatrix clearing the result but till then it referring the same object so empty arraylist was getting added so used the clone the method from objeect class for making the deep copy or new object
//below will fixed this issue
newcorrelationMatrix.add((ArrayList<Double>) lastCorrelationMatrix.clone());
lastCorrelationMatrix.clear();
I have coded below the and am just used dummy version of fincdCorrelation so please use yours.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
class Correlation {
public static void main(String[] args) {
ArrayList<ArrayList<Double>> correlationMatrix = new ArrayList<ArrayList<Double>>();
//created some random variable
Double [] d1 = {3.5,5.4,6.7,6.2};
Double [] d2 = {5.6,7.9,4.4,2.9};
Double [] d3 = {5.2,9.5,5.1,3.9};
Double [] d4 = {6.8,3.2,8.1,4.7};
correlationMatrix.add(new ArrayList<Double>(Arrays.asList(d1)));
correlationMatrix.add(new ArrayList<Double>(Arrays.asList(d2)));
correlationMatrix.add(new ArrayList<Double>(Arrays.asList(d3)));
correlationMatrix.add(new ArrayList<Double>(Arrays.asList(d4)));
ArrayList<ArrayList<Double>> newcorrelationMatrix = new ArrayList<ArrayList<Double>>();
ArrayList<Double> lastCorrelationMatrix = new ArrayList<Double>();
for (int i = 0; i < correlationMatrix.size(); i++) {
for (int j = 0; j < correlationMatrix.size(); j++) {
lastCorrelationMatrix.add(findCorrelation(correlationMatrix.get(i), correlationMatrix.get(j)));
}
newcorrelationMatrix.add((ArrayList<Double>) lastCorrelationMatrix.clone());
lastCorrelationMatrix.clear();
}
//checking the contents of new newcorrelationMatrix
for(ArrayList<Double>d:newcorrelationMatrix)
System.out.println(d);
}
//you might need to use your findCorrelation method it just dummy one
private static Double findCorrelation(ArrayList<Double> a1, ArrayList<Double> a2){
return new Random().nextDouble();
}
}
//OUTPUT
[0.5983171226435918, 0.9218663020064187, 0.19201455580242988, 0.04402195086540317]
[0.2620192566548277, 0.5842034458799858, 0.023179928657071303, 0.3303204685135067]
[0.5663561056877091, 0.32605940809431333, 0.39262783823084946, 0.9258185504035888]
[0.464581514143835, 0.7820980934752744, 0.26478008503018147, 0.8301987887207712]