You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

MQL5数组越界错误排查及数组输出实现技术求助

Fixing "Array Out of Range" Error and Outputting Array Content in MQL5

Let's break down your issue and fix it step by step:

Why the "Array Out of Range" Error Happens

You're seeing this error because:

  1. You declared a dynamic array string d[] but never allocated space for its elements. MQL5 dynamic arrays start empty, so trying to access d[j] immediately throws an out-of-bounds error.
  2. Calling ArraySetAsSeries(d, true) sets the array to use reverse indexing (the most recent element is at the highest index). This clashes with your forward for loop (j=0 to 4), since reverse arrays don't allow valid access to lower indices until the higher ones are populated.

Fixed Code with Array Output

Here's the corrected version that eliminates the error and prints the array content to the test log:

void OnInit() {
 int i = 0;
 string d[];
 // First, resize the array to hold 5 elements (matches your loop's j range 0-4)
 ArrayResize(d, 5);
 
 while(i < 2) {
  // Remove ArraySetAsSeries unless you specifically need reverse indexing (e.g., for price data)
  // ArraySetAsSeries(d, true);
  
  for (int j=0; j<5; j++) {
   if(MathMod(j,2)==0 && i==0) 
      d[j] = "even";
   else if(MathMod(j,2)==0 ) 
      d[j] = "even without i zero";
   else 
      d[j] = "odd";
  }
  
  // Print the array content for each iteration
  Print("--- Iteration ", i, " Array Content ---");
  // Option 1: Loop through each element and print
  for(int k=0; k<ArraySize(d); k++){
     Print("d[", k, "] = ", d[k]);
  }
  // Option 2: Use MQL5's built-in PrintArray for quicker output
  // PrintArray(d, 0, ArraySize(d), "Full Array (Iteration ", i, ")");
  
  i++;
 }
}

Key Fixes Explained

  • ArrayResize(d, 5): This allocates space for 5 elements in the dynamic array, matching your loop's range of j from 0 to 4. Without this, the array has no valid indices to access.
  • Removed ArraySetAsSeries: Unless you need reverse indexing (common for working with historical price data), this line is unnecessary here and causes conflicts with your forward loop. If you do need reverse indexing, see the alternative version below.
  • Added Output Logic: The Print() loop (or PrintArray) writes each element's value to the test log, so you can verify the array content during testing.

If You Need Reverse Indexing

If your use case requires ArraySetAsSeries(d, true), adjust the loop to work with reverse indices (highest index first):

void OnInit() {
 int i = 0;
 string d[];
 ArrayResize(d, 5);
 
 while(i < 2) {
  ArraySetAsSeries(d, true);
  // Reverse arrays use highest index first (4 down to 0 for size 5)
  for (int j=ArraySize(d)-1; j>=0; j--) {
   // Calculate original position to check even/odd (since j is reversed)
   int originalPos = ArraySize(d)-1 - j;
   if(MathMod(originalPos,2)==0 && i==0) 
      d[j] = "even";
   else if(MathMod(originalPos,2)==0 ) 
      d[j] = "even without i zero";
   else 
      d[j] = "odd";
  }
  
  Print("--- Iteration ", i, " Reversed Array Content ---");
  for(int k=ArraySize(d)-1; k>=0; k--){
     Print("d[", k, "] = ", d[k]);
  }
  
  i++;
 }
}

Testing the Fix

When you run the test, you'll see the array content printed in the Experts tab log, like this:

--- Iteration 0 Array Content ---
d[0] = even
d[1] = odd
d[2] = even
d[3] = odd
d[4] = even
--- Iteration 1 Array Content ---
d[0] = even without i zero
d[1] = odd
d[2] = even without i zero
d[3] = odd
d[4] = even without i zero

内容的提问来源于stack exchange,提问作者Jaffer Wilson

火山引擎 最新活动