Bartłomiej Sobieszek
Bartłomiej Sobieszek

Reputation: 2800

Out of range in searching function?

    public static int PlanetToIndex(string planetToSearch, List<Planet> pl)
    {
        for (int i = 0; i < pl.Capacity; i++) // out of range...
            if (pl[i].Equals(planetToSearch))
                return i;

        return -1;
    }

Its C# simple search function, it seems that Capacity = 16, but range is exceeded at 14...

14 is right value, why MessageBox shows 16?!

Also, there you have List what I pass to function (Planet is abstract class)

class Galaxy
{
    public List<Planet> galaxy = new List<Planet>();
    Planet Ceres = new Planet.Ceres();
    Planet Pluto = new Planet.Pluto();
    Planet Europa = new Planet.Europa();
    Planet Venus = new Planet.Venus();
    Planet Sedna = new Planet.Sedna();
    Planet Jupiter = new Planet.Jupiter();
    Planet Void = new Planet.Void();
    Planet Eris = new Planet.Eris();
    Planet Mars = new Planet.Mars();
    Planet Uranus = new Planet.Uranus();
    Planet Neptune = new Planet.Neptune();
    Planet Saturn = new Planet.Saturn();
    Planet Earth = new Planet.Earth();
    Planet Mercury = new Planet.Mercury();

    public Galaxy()
    {
        galaxy.Add(Ceres);
        galaxy.Add(Pluto);
        galaxy.Add(Europa);
        galaxy.Add(Venus);
        galaxy.Add(Sedna);
        galaxy.Add(Jupiter);
        galaxy.Add(Void);
        galaxy.Add(Eris);
        galaxy.Add(Mars);
        galaxy.Add(Uranus);
        galaxy.Add(Neptune);
        galaxy.Add(Saturn);
        galaxy.Add(Earth);
        galaxy.Add(Mercury);
    }
}

Upvotes: 0

Views: 84

Answers (5)

KMC
KMC

Reputation: 20036

Unlike an array which is fixed in size, List resizes as it grows. List<T>.Capacity is the property that allows you to get/set the size of the list before resizing is needed.

See MSDN List.Capacity:

Capacity is the number of elements that the List can store before resizing is required, while Count is the number of elements that are actually in the List. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.

Hence, in your example, Capacity > Count

Upvotes: 0

fcuesta
fcuesta

Reputation: 4520

I think you want to use pl.Count, instead of pl.Capacity.

Capacity is the total number of elements the internal data structure can hold without resizing. While Count is the number of elements actually contained in the List.

Upvotes: 1

Craig
Craig

Reputation: 94

In your for loop, you need to use

for (int i = 0; i < pl.Count; i++)

Capacity is the amount of elements it has space available to store. Try this just to see what the difference is:

        List<int> intlist = new List<int>();
        intlist.Capacity = 32;
        Console.WriteLine(intlist.Count);
        Console.WriteLine(intlist.Capacity);
        Console.In.Read();

The output is 0 and 32. Because there are actually no elements in the array, but space for 32 elements.

Upvotes: 0

CSJ
CSJ

Reputation: 3951

Capacity is not the same as Count. Try this:

for (int i = 0; i < pl.Count; i++)
    if (pl[i].Equals(planetToSearch))
        return i;

or better yet:

return pl.FindIndex(planet => planet.Equals(planetToSearch));

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13599

I would recommend using count

 for (int i = 0; i < pl.Count(); i++) 

Upvotes: 0

Related Questions