locked
Index out of range exception with arrays RRS feed

  • Question

  • I am building a single-dimensional array and when I try to iterate through the array and print out the values using a while loop using the .Length function I get an index out of range exception. Sure I could tell the while loop to stop when i reaches a fixed value... but I would like to learn how to use the .Length function properly.

     

    Here is the code I have:

     

    int[] singleArray2 = { 12, 32, 16, 23, }; // new array holding predefined values
    
          int i = 0;
          while (i < singleArray1.Length)
          {
            Console.WriteLine(singleArray2[i]);
            i++;
          }

     

    I appreciate the help

    Friday, August 13, 2010 6:50 AM

Answers

  • Thanks a lot everyone, sorry for the late reply I lost track of the thread and just now found it again. I had no idea that I had used one array in the while condition but tried to print out values from another array inside the loop. So of course thats going to cause problems. I made the change but still the out of bounds exception exists.

    I went with the foreach method for printing the values... works and I would believe this is a prefered method of doing it seeing as microsoft created a tutorial on using the foreach with arrays.

     

    Thanks a lot everyone

    • Marked as answer by surreal5335 Sunday, August 15, 2010 8:56 PM
    Sunday, August 15, 2010 8:56 PM

All replies

  • hi,

     

    there is a conflict with while condition (connected with singleArray1) and iterators (connected with singleArray2). i'm guessing the length of singleArray2 and singleArray1 differs

     

    here you can read more about tables/arrays:

    http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx

    http://dotnetperls.com/int-array


    BR, Karol. mark as answer/vote as helpful if it helped you
    Friday, August 13, 2010 6:59 AM
  • I guess the problem is you using singleArray1 in the while statement, but printing values form singleArray2. So correct the appropriate statement to (i < singleArray2.Length)

    • Proposed as answer by Eyal Solnik Friday, August 13, 2010 7:12 AM
    Friday, August 13, 2010 7:03 AM
  • Besides the array name typho i have another suggestion. You could use foreach to loop through the elements in an array.

        foreach (int value in SingleArray2)
        {
          Console.WriteLine(value);
        }

    Friday, August 13, 2010 7:09 AM
  • This is all you need:

        static void Main(string[] args)
        {
          int[] singleArray2 = { 12, 32, 16, 23, };
          for (int i = 0; i < singleArray2.Length; i++)
          {
            Console.WriteLine("{0}", singleArray2[i]);
          }
          Console.ReadLine();
        }
    

    In case there is another array  in your project (becuase in your example are 2 array - singleArray1 and singleArray2 mentioned, even singleArray1 is no where declared), simple put some more code here, and we will help ypu out.

    Friday, August 13, 2010 7:10 AM
  • int[] singleArray2 = { 12, 32, 16, 23, }; // new array holding predefined values
    
       int i = 0;
       while (i < singleArray2.Length)  //use singleArray2 instead of singleArray1
       {
        Console.WriteLine(singleArray2[i]);
        i++;
       }

    will solve you problem

    Nanda - Misys Software Solutions,Bangalore
    Friday, August 13, 2010 7:10 AM
  • Thanks a lot everyone, sorry for the late reply I lost track of the thread and just now found it again. I had no idea that I had used one array in the while condition but tried to print out values from another array inside the loop. So of course thats going to cause problems. I made the change but still the out of bounds exception exists.

    I went with the foreach method for printing the values... works and I would believe this is a prefered method of doing it seeing as microsoft created a tutorial on using the foreach with arrays.

     

    Thanks a lot everyone

    • Marked as answer by surreal5335 Sunday, August 15, 2010 8:56 PM
    Sunday, August 15, 2010 8:56 PM