简体   繁体   中英

Index outside of the bounds of the array in a list

I have the following code:

 public List<IAction> Dispatch(string[] arg)
   {
       int time=0;
       int i = 0;
       int j = 0;
       List<IAction> t = new List<IAction>(10);
       do
       {
           if (arg[j][0] == '/') // I get index out of bounds here
           {
               Options opt = new Options();                   

               time = opt.Option(arg[j]);
               j++;
           }
           else
           {
               if (int.Parse(arg[j]) >= 0 && int.Parse(arg[j]) <= 20)
               {
                   t.Add(new ComputeParam(int.Parse(arg[j])));
                   i++;
                   j++;                      
               }
           }

       } while (i != arg.Length);
       for (int z = 0; z < t.Count; z++)
       {
           ((ComputeParam)t[z]).Time = time;
       }
       return t;
   }

Why does the error happen... I just pass the arguments and if they are numbers I add them to a list, if not I set an option and move on. What is the problem here?

Edit: I pass 2 /t:Med 2 3 those are the arguments. I aleready checked it arg[1] (in this case) is null, but it's not.

I see a couple of possible issues here:

  1. If arg[] is empty, you will get the exception
  2. If arg[j] is an empty string, you will get the exception
  3. If you have any options, you will get the exception on a later execution of the loop, because j is being incremented, but i is not.

I think this will fix it:

public List<IAction> Dispatch(string[] arg)
{
   int time=0;
   List<IAction> t = new List<IAction>(10);
   for (int j = 0; j < arg.Length; j++)
   {
       if (!String.IsNullOrEmpty(arg[j]) && arg[j][0] == '/')
       {
           Options opt = new Options();                   

           time = opt.Option(arg[j]);
       }
       else
       {
           if (int.Parse(arg[j]) >= 0 && int.Parse(arg[j]) <= 20)
           {
               t.Add(new ComputeParam(int.Parse(arg[j])));
               // Don't need to increment i                
           }
       }

   }
   for (int z = 0; z < t.Count; z++)
   {
       ((ComputeParam)t[z]).Time = time;
   }
   return t;
}

You get this when you try to index an element where there is no element, as in, usually you are indexing elements when the array is smaller than you expect.

In your case, either:

  • The value of j is greater than or equal to arg.Length so arg[j] throws out of bounds.
  • The string in arg[j] has no characters so arg[j][0] throws out of bounds.

You can test the length using the Length property of arrays. string also has a Length property.

Just as an aside, you don't increment j in all cases and you don't even seem to use i other than in the check, but j could have been incremented further than i meaning your while (i.= args.Length) won't defend you against IndexOutOfBoundsException . Also, the check should at least be while (i < args.Length)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM