-1

I'm just starting to learn c#. I Have a problem with List and I can't resolve it: I need to genereate a list if "individs". Each inidivid is a sequence of the integers. (I'm solving here travelling salesman problem using Genetic Algorithm) For example, I have a class inidivid:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TravellingSalesman
{
    public class individ
    {
    public int[] individSequence { set; get; }
    public int fitnessFunction { set; get; }
    public individ(int size)
    {
        individSequence = new int[size];
        individSequence = RandomNumbers(size).ToArray(typeof(int)) as int[];

    }

    public ArrayList RandomNumbers(int max)
    {
        // Create an ArrayList object that will hold the numbers
        ArrayList lstNumbers = new ArrayList();
        // The Random class will be used to generate numbers
        Random rndNumber = new Random();

        // Generate a random number between 1 and the Max
        int number = rndNumber.Next(1, max + 1);
        // Add this first random number to the list
        lstNumbers.Add(number);
        // Set a count of numbers to 0 to start
        int count = 0;

        do // Repeatedly...
        {
            // ... generate a random number between 1 and the Max
            number = rndNumber.Next(1, max + 1);

            // If the newly generated number in not yet in the list...
            if (!lstNumbers.Contains(number))
            {
                // ... add it
                lstNumbers.Add(number);
            }

            // Increase the count
            count++;
        } while (count <= 10 * max); // Do that again

        // Once the list is built, return it
        return lstNumbers;
    }
}

And now I want to create a list of this object: List list; ... in c-tor: list = new List();

and now I'm trying to add objects to the list and get them for future work

private void createFirstGeneration()
{
    for (int i = 0; i != commonData.populationSize; ++i)
    {
        individ newIndivid = new individ(commonData.numberOfcities);
        list.Add(newIndivid);

            for (int j = 0; j != commonData.numberOfcities; ++j)
                System.Console.Write(((individ)list[i]).individSequence[j]);
            System.Console.WriteLine();

    }
    for (int i = 0; i != commonData.populationSize; ++i)
    {
        for (int j = 0; j != commonData.numberOfcities; ++j)
            System.Console.Write(((individ)list[i]).individSequence[j]);
        System.Console.WriteLine();
    }
}

commonData.populationSize is a number of inidivids in the population. But I Have different output from the two outputs from this example.

312
213
213
213
213
312
213
213
213
213

I'm a newbie in c#, so,please, can you help me?

H H
  • 263,252
  • 30
  • 330
  • 514
user3014909
  • 41
  • 2
  • 12

1 Answers1

0

If i just go by the code in second half of your question (it does have missing variable like commonData, populationSize & numberOfCities)

The first "for" loop is adding item to the list within the loop scope itself

individ newIndivid = new individ(commonData.numberOfcities);
list.Add(newIndivid); ----> This line

So although you are looping through the same "commonData.PopulationSize" the list count/content are not same in both loops.

Anupam Yadav
  • 4,353
  • 4
  • 19
  • 20