0

I am trying to implement zip function of python(Zip function details) in java on ArrayList of ArrayLists. I have two ArrayList of Arraylists 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)
dudel
  • 684
  • 5
  • 15
Pranay
  • 3
  • 1
  • 5
  • Have you seen [this](http://stackoverflow.com/questions/2118932/is-there-an-accepted-java-equivalent-to-pythons-zip) or [this](http://stackoverflow.com/questions/3833814/java-how-to-write-a-zip-function-what-should-be-the-return-type)? – pathfinderelite Nov 05 '16 at 13:13
  • Zip function of Functional java is not working on List of Lists and the 2nd version is working a little bit and producing output in following manner [[org.apache.commons.math3.distribution.BinomialDistribution@2f92e0f4,org.apache.commons.math3.distribution.BinomialDistribution@28a418fc],[0.6,0.4],[org.apache.commons.math3.distribution.NormalDistribution@5305068a,org.apache.commons.math3.distribution.NormalDistribution@1f32e575],[0.5,0.4]] – Pranay Nov 05 '16 at 14:49

0 Answers0