I am trying to implement zip function of python(Zip function details) in java on ArrayList
of ArrayList
s. I have two ArrayList
of Arraylist
s as show below:
static ArrayList<ArrayList<Object>> distributions = new ArrayList<ArrayList<Object>>();
static ArrayList<ArrayList<Object>> probabilities = new ArrayList<ArrayList<Object>>();
with following data
distributions= [[org.apache.commons.math3.distribution.BinomialDistribution@2f92e0f4, org.apache.commons.math3.distribution.BinomialDistribution@28a418fc], [org.apache.commons.math3.distribution.NormalDistribution@5305068a, org.apache.commons.math3.distribution.NormalDistribution@1f32e575]] probabilities = [[0.6, 0.4], [0.5, 0.4]]
I wrote following piece of code
static ArrayList<ArrayList<Object>> zipped = new ArrayList<ArrayList<Object>>();
int count=0;
for(int i=0;i<distributions.size();i++){
for(int j=0;j<Math.max(distributions.get(i).size(), probabilities.get(i).size());j++){
zipped.get(count).add(distributions.get(i).get(j));//during i=0 j=0 count =0 i am adding 1st element of the 1st sub arraylist of distributions arraylist to the 1st sub arraylist as a 1st element to the zipped arraylist and so on.
zipped.get(count).add(probabilities.get(i).get(j));//during i=0 j=0 count =0 i am adding 1st element of the 1st sub arraylist of probabilities arraylist to the 1st sub arraylist as a 2nd element to the zipped arraylist and so on.
if(count<(distributions.size()+probabilities.size())){
count++;
}
}
}
System.out.println(zipped);
I want the output as show below:
[[org.apache.commons.math3.distribution.BinomialDistribution@2f92e0f4,0.6],[org.apache.commons.math3.distribution.BinomialDistribution@28a418fc,0.4],[org.apache.commons.math3.distribution.NormalDistribution@5305068a,0.5],[org.apache.commons.math3.distribution.NormalDistribution@1f32e575,0.4]]
Can anyone please help in getting the above output. I am getting the following exception
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at SamData.testData(SamData.java:45)
at SamData.main(SamData.java:85)