Sarabjeet Singh Sarabjeet Singh - 1 year ago 107
Java Question

Build an array from existing arrays?

int[] StarTime = new int[20];
int[] duration = new int[40];
int[] EndTime = new int[StarTime.length];
StarTime[0] = 0;

ExponentialDistribution exp = new ExponentialDistribution(4.0);
for(int j = 1; j < 20; j++){
StarTime[j] = (int)exp.sample() + 1+StarTime[j-1];

for(int k = 0; k < 20;k ++){
duration[k] = 20 + (int)(Math.random() * ((120 - 10) + 1));

I have two arrays StarTime and duration. I want to build an array whose each index is assigned the value which is sum of the values of the indexes of these two arrays. Like suppose EndTime is the array I want to create and if StarTime[0] is 2 and duration[0] is 4 and EndTime[0] should be 6.

How should I do this?

Answer Source

There is no reason for giving type mismatch, while you are adding two int array into another int array. It should work:

for(int k = 0; k < 20; k++){
    EndTime[k] = StarTime[k] + duration[k];

Ensure that exp.sample() is really casting into int. Also ensure the arrays are of same type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download