当您遍历二维数组中每个数组的第一个元素时,如何避免java.lang.ArrayIndexOutOfBoundsException?

时间:2018-09-14 09:55:17

标签: java multidimensional-array

我正在为具有4个服务器且每个服务器具有4个VM的云计算服务器进行Markov链模拟。

在我的情况下,服务器是数组

  

{4,1,0}

繁忙的虚拟机是此阵列的第一个变量,因此为4。 现在,当我们到达时,我们检查所有服务器是否有4个VM处于繁忙状态,如果是,我们丢弃数据包,以增加我的情况下的损失变量,否则我将检查第一台服务器是否有4个VM处于繁忙状态。如果第二台服务器的VM都不忙,它将进入第二台服务器。现在,在第二个迭代中,我可以离开,如果是这种情况,则可能会在具有至少一个VM的每台服务器上发生偏离。我使用概率公式进行检查,但我不想在此处详细输入。

现在我要解决上述系统问题,如下所示:

在仿真开始时,我有一个2D阵列

  

[{4,1,0},{3,1,0},{0,1,0},{0,1,0}]

     

//第一次迭代

现在,我想检查一下是否所有数组的第一个元素都是4,那么我将为代码添加一个名为

的变量。
  

损失

否则,我将检查数组的第一个元素是否为4,如果为4,那么我将继续检查数组的第二个元素,如果在我上面的例子中,数组的第二个元素不是4然后我将其增加1,我想要得到结果

  

[{4,1,0},{4,1,0},{0,1,0},{0,1,0}]

现在在接下来的4次迭代中,“如果我总是有到达数据包而没有离开”,我希望获得如下输出

  

[{4,1,0},{4,1,0},{1,1,0},{0,1,0}]   [{4,1,0},{4,1,0},{2,1,0},{0,1,0}]   [{4,1,0},{4,1,0},{3,1,0},{0,1,0}]   [{4,1,0},{4,1,0},{4,1,0},{0,1,0}]

但是,与此同时,我仍然必须检查第一个数组和第二个数组的第一个变量,因为如果发生偏离,由于我的代码的其他部分而导致的这些值可能会减小。

  

//第二次迭代(出发)

因此,假设我在第二个迭代中有一个偏离,第一次迭代后我的2D数组的状态为

  

[{4,1,0},{4,1,0},{0,1,0},{0,1,0}]

现在发生了一次起飞,假设发生在第二台服务器上,我的状态将再次作为起点

  

[{4,1,0},{3,1,0},{0,1,0},{0,1,0}]

     

//第三次迭代(到达)

现在我必须检查第一个数组的第一个元素,因为它是4,所以我不能递增该元素,所以我必须检查第二个数组的第一个元素,因为它不是4,所以我必须将其增加1。

现在,在下面的Java代码中,当我检查数组中每个元素的第一个是否为4时,我遇到了 java.lang.ArrayIndexOutOfBoundsException (我知道这是什么)。 ,如果为4,则必须检查第二个数组,以此类推

第二个问题是,当我为10万次复制(迭代)运行我的实现时,离开的数量比到达的数量多

我真的需要一些帮助。

MainSimulation markovChain= new MainSimulation();

//statistics
int loss=0;
int arrivals=0;
int k=0;
int failures=0;
int departures=0;

//initializations
int numberOfReplications=100000;
int[] currentStateServer1= {4,1,0}; // this means Server 1 is working and all its VMs are busy
int[] currentStateServer2= {3,1,0};
int[] currentStateServer3= {0,1,0};
int[] currentStateServer4= {0,1,0};

int[][] currentState= {currentStateServer1,currentStateServer2,currentStateServer3,currentStateServer4};

for(int i=0;i<numberOfReplications;i++) {

    // generate a random number uniformly distributed between 0.0 and 1.0
    double uniformRandomNumber=randomNumber.nextDouble(); 
    System.out.println("The random number is" +uniformRandomNumber);

    //Arrival
    if (uniformRandomNumber<lambda/markovChain.sum(currentState)){

        System.out.println("We have arrival");
        if(currentState[0][0]==4 && currentState[1][0]==4 && currentState[2][0]==4 &&currentState[3][0]==4) {
            // the system is full, arrivals get lost
            loss++;
        } else {
            /*
             * test the current state 2D array to check if any server have all VMs busy
             *  If yes, check the next server and so on
             */
             for(int j=0;j<4;j++) {
                 if(currentState[j][0]==4) {
                     //if true, check the next server if all its VMs are busy
  //Here I have a RunTime error
                     currentState[j][0]=currentState[j+1][0];
                     System.out.println("The currentState is "+currentState[j][0]);
                 } else {
                     /*
                      *  if false, then this server has a free VM and the VM requests will occupy this server
                      *  the state of this server(the first variable) in this case will be increased by one
                      */
                      currentState[j][0] = currentState[j][0]+1;
                  } // close if block, used for checking or incrementing the state of each server   

              } // close for used for checking or incrementing the state of each server to the next one
          } // close the if block which check if the system if full or not
          arrivals++;
      } //close arrival

      //Departure   
      //departure check for the first server
      else if(uniformRandomNumber<(lambda+mu*currentState[0][0])/markovChain.sum(currentState)) {

          departures++;
          currentState[0][0]=currentState[0][0]-1;
      } //close the "check if" departure for first server
         // departure check for the second server
      else if(uniformRandomNumber<(lambda+mu*(currentState[0][0]+currentState[1][0]))/markovChain.sum(currentState)){

          departures++;
          currentState[1][0]=currentState[1][0]-1;
      } //close the "check if" departure occurs for the second server
        // departure check for the third server
      else if(uniformRandomNumber<(lambda+mu*(currentState[0][0]+currentState[1][0]+currentState[2][0]))/markovChain.sum(currentState)) {

          departures++;
          currentState[2][0]=currentState[2][0]-1;
       } //close the "check if" departure occurs for the third server
       else if(uniformRandomNumber<(lambda+mu*(currentState[0][0]+currentState[1][0]+currentState[2][0]+currentState[3][0]))/markovChain.sum(currentState)) {

            departures++;
            currentState[3][0]=currentState[3][0]-1;
        } //close the "check if" departure occurs for the third server
           // close departure

    //When I system.out.println the number of arrivals and departures I have more departures

1 个答案:

答案 0 :(得分:0)

您的第一个问题是一个组合: 首先,您不需要预先检查这4个服务器中是否有一个具有完整的VM,您已经在for循环中进行了检查。

第二,检查当前状态是否为4超级怪异。没有必要用以下当前状态重新分配当前状态:

for(int j=0;j<4;j++) {
    if(currentState[j][0]==4) {                       
    //Here I have RunTime error
        currentState[j][0]=currentState[j+1][0]; //this is no logical
        System.out.println("The currentState is "+currentState[j][0]);
    }

最好这样做:

boolean noLoss=false;
for(int j=0;j<4;j++){
    if(currentState[j][0]==4)
        continue; //if current server has no vms free, chck the next server
    else{
        currentState[j][0]++; //
        arrival++; //the package got assigned to a server, so it arrived
        noLoss=true; //the package was able to arrive to a server
        break; //we don't need to check any other server, since the package arrived
    }
}
if(!noLoss)
    loss++; //the package couldn't find a free server, hence it is a loss

这些行不再需要:

if(currentState[0][0]==4 && currentState[1][0]==4 && currentState[2][0]==4 &&currentState[3][0]==4) {
    // the system is full, arrivals get lost
    loss++;
} else{                  
    for(int j=0;j<4;j++) {
        if(currentState[j][0]==4) { 
            currentState[j][0]=currentState[j+1][0];
            System.out.println("The currentState is "+currentState[j][0]);
        }
        else {                
            currentState[j][0] = currentState[j][0]+1;
        } // close if block, used for checking or incrementing the state of each server   
    } // close for used for checking or incrementing the state of each server to the next one
} // close the if block which check if the system if full or not
arrivals++;

第二个问题:我无法追溯您的lambdamarkovChain对象的确切含义。如果有离开,您需要检查到达的数量(只有到达的数据包可以离开吗?)。否则,这是完全随机的(并且取决于您的if语句)。因此,如果出发次数过多,首先是因为您没有在计算中包括到达次数和/或您的markovChain不正确。

编辑1 : 如果您以任何一种方式(丢失或未丢失)计算到达的包裹,那么肯定需要将到达的包裹放在for循环之外

boolean noLoss=false;
for(int j=0;j<4;j++){
    if(currentState[j][0]==4)
        continue; //if current server has no vms free, chck the next server
    else{
        currentState[j][0]++;
        noLoss=true; //the package was able to arrive to a server
        break; //we don't need to check any other server, since the package arrived
    }
}
if(!noLoss)
    loss++; //the package couldn't find a free server, hence it is a loss
arrived++;