0

I'm trying to make a List of about 5500 teams and create an object Team for each element of the list using this code I got from a tutorial:

    public Form1()
    {
        InitializeComponent();
        List<Team> teams = new List<Team>();
        teams.Capacity = 5500;            

        for (int y = 1; y <= 5500; y++)
        {
            teams[y] = new Team(y);
        }
    }

But I keep getting this error:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in [my program].exe

Additional information: Index was outside the bounds of the array.

In this program, "Team" is a custom class that requires a team number when created (...new Team([teamnumber])).

Each teamnumber is their unique identity, so this has to correspond with each team's index in the list.

...teams[y] = new Team(y);...

What I'm trying to do is make sure there's a Team object created for every element in teams so that I don't run into errors later when trying to add an attribute to a certain element. Also, I don't want a "Team 0", this is why I started y at 1 in the for loop. I've also tried using a foreach loop but I get the same error.

I'm using Visual Studio 2012 Express. This is a Windows Presentation Forms program written in C#.

3
  • If you really want an array, use Team[] teams = new Team[5500];. But you can use a List<Team>, just use Add method to append items to your list. Note that both List<T> and T[] are indexed from 0 through N-1, not 1 through N. Commented Jan 5, 2014 at 23:06
  • I used a List because I find lists to be more accessible than normal arrays. Thank you all for your help! Commented Jan 5, 2014 at 23:23
  • Yeah, lists are usually better. I misunderstood because you mentioned "array trouble" in the header, but I realize that was maybe simply a reference to the exception message. Note: If you like Linq, you can consider var teams = Enumerable.Range(1, 5500).Select(y => new Team(y)).ToList();. Commented Jan 6, 2014 at 0:05

3 Answers 3

4

You are accessing an empty List<>, any access will throw this exception. Setting the capacity will not create empty entries.

var list = new List<int>(10); // Capacity = 10 but Count = 0
list[0] = 1; // out-of-range exception is thrown

You need to Add() elements before you can use teams[index]

for (int y = 1; y <= MAX_TEAMS; y++)
{
   teams.Add(new Team(y));  
}
Sign up to request clarification or add additional context in comments.

Comments

1

Arrays are indexed starting from zero, so teams[5500] is the 5501st element of the list. The list has capacity of 5500 so that's out of range.

You could increase the Capacity to 5501, or use new Team(y+1) for y from 0 to 5499.

Also, you can get rid of the line where you set the capacity by using a different constructor:

List<Team> teams = new List<Team>(5500);

Comments

1
public Form1()
{
    InitializeComponent();
    List<Team> teams = new List<Team>(5500);

    for (int y = 1; y < 5501; y++)
    {
        teams.Add(new Team(y));
    }
}

This will work. This is how you initialize and add items to the list. One of the things to notice is y < 5501 vs y <= 5500. <= is a little bit of performance hit because this is OR condition on operator.

Initializing the list's capacity is the good idea because it will lead to better performance since list will not need to resize.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.