-1
public class EconomicModel {

    public static void main(String[] args) {

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

        int[] ResAllocRAM = {101,9,56,89,43,30,8,60,13,41,60, 20, 32,71,76,7,31,46,30,99 };


        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));

            }


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

            }
            Random r = new Random();
            for (int i=0; i<20; i++){
                relinqtime[i] = r.nextInt((EndTime[i] - StarTime[i])+ StarTime[i] ) + StarTime[i];

            }
            for ( int j = 0; j<20; j++)
            for(int k =  StarTime[j]; k <= relinqtime[j]; k++){
                 TotalDuration[k]+= ResReqRAM[j];

            }
            }
            }   

I have arrays called StarTime, relinqtime, ResAllocRAM and TotalDuration. I want to assign values to the indexes of array TotalDuration based on the other three arrays. Like if value of StarTime[1] is 5, value of relinqtime[1] is 8, value of ResAllocRAM[1] is 9. I want that value TotalDuration[5],TotalDuration[6],TotalDuration[7] should be the value of ResAllocRAM[1].

I tried the above code but it is giving exception bound error at line TotalDuration[k]+= ResReqRAM[j];

What should I do to map the values of these arrays and produce a new array as mentioned?

6
  • Welcome to Stack Overflow! Please format your question code better so it is readable without scrolling, show specific and literal error messages, and explain in more detail how you want the values to be calculated.
    – vz0
    Aug 30, 2016 at 9:06
  • 1
    And please, indent your code properly, and respect the Java naming conventions.
    – JB Nizet
    Aug 30, 2016 at 9:10
  • Note: According to the Java language conventions, you should name variables with names that start with a lowercase letter, e.g. endTime, startTime, not EndTime and StarTime - uppercase letter at the beginning is reserved for types (classes, enums, interfaces). Aug 30, 2016 at 9:10
  • Does anyone understand the requirements?
    – shmosel
    Aug 30, 2016 at 9:11
  • r.nextInt(EndTime[i]) is a shortcut for r.nextInt((EndTime[i] - StarTime[i])+ StarTime[i] ) Aug 30, 2016 at 9:28

2 Answers 2

0

Use a Map, it saves elements as Key, Value pair, remember keys should be unique.

Map<Integer, Integer> hashMap = new HashMap<>();
hashMap.put(TotalDuration[5], ResAllocRAM[1]); //put(key,value)
int value = hashMap.get(TotalDuration[5]); //it will return you value of ResAllocRAM[1]

simple tutorial for map

4
  • Can one key have multiple values?
    – Sjs
    Aug 30, 2016 at 9:27
  • @Sjs No. However, a value can be a Collection or Array and in that sense hold multiple values within itself.
    – Vasan
    Aug 30, 2016 at 9:36
  • for having multiple value for a key add arrays as values i.e. Map<Integer, Integer[]> hashMap = new HashMap<>();
    – Fahad
    Aug 30, 2016 at 9:42
  • detail example: Map<Integer, Integer[]> hashMap = new HashMap<>(); Integer[] array = {12, 38, 49, 46}; Integer yourKey = 32; hashMap.put(yourKey, array);
    – Fahad
    Aug 30, 2016 at 9:45
0

Try changing the line as follows

TotalDuration[k] = ResReqRAM[j];

If you are trying to increment TotalDuration then set initial values to 0 as the TotalDuration array is empty when you are trying to assign in it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.