954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Use of List<int[]>

Hello. I am writing a program that uses a List object to retain a list of int[] objects.

private int[] numbers = new int[9];
private List<int[]> theList = new List<int[]>();


It's basicaly a backtracking algorithm that constructs thenumbers object, which contains a permutatio of the numbers 1..9, and after every complete construction adds the solution to the list:

list.Add(numbers);


I checked the construction and it works fine, but if I want to read/print/use the list after all the solutions are added, it only contains the number 9, in every entry of the integer vector.

My question is: why isn't the List object saving the exact state added to each of it's elements? Here is my code (I know it's messy, but it's experimental):

private int[] numbers = new int[9];
        private List<int[]> theList = new List<int[]>();

        public Executor()
        {
        }

        private void BackTrack(int p)
        {
            int i;
            for (i = 1; i <= 9; i++)
            {
                numbers[p] = i;
                if (CheckCorrectness(p))
                {
                    if (p == 8) AddSolution();
                    else BackTrack(p + 1);
                }
            }
        }

        private bool CheckCorrectness(int p)
        {
            int i;
            for (i = 0; i < p; i++)
                if (numbers[i] == numbers[p]) return false;
            return true;
        }

        private void AddSolution()
        {
            theList.Add(numbers);
            //uncomment line to that it generates the right solution
            //for (int i = 0; i < 9; i++) Console.Out.Write(numbers[i] + " ");
        }
       
        public void Print()
        {
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Out.Write(theList[i][j].ToString() + " ");
                }
                Console.Out.WriteLine();
            }
            Console.Out.WriteLine();
        }

It should print:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
and so on, but it prints:
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9

It starts execution withBackTrack(0) and the it calls the Print() method.

andreivanea
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

What about list.Count? I think it should be theList.Count.

__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
What about list.Count? I think it should be theList.Count.


The Count is 362880. It successfully adds all the permutations, but when the algorithm is over, theList is a list of 362880 {9 9 9 9 9 9 9 9 9} elements.

andreivanea
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Lines 16 and 17 of your code never get executed, because the function on line 22 always returns false. Play computer like I did! It's fun! You will see why your int array fills up with all nines!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Lines 16 and 17 of your code never get executed, because the function on line 22 always returns false. Play computer like I did! It's fun! You will see why your int array fills up with all nines!


You're wrong, the function executes fine. It's easy to see if you uncomment the code at line 34, or simply if you add a breake point at line 35, to see the full array.

The problem doesn't seem that the problem is at the generation of the permutation, but rather at the saving/storing section, envolvind the List object.

andreivanea
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Your code adding same reference of an array into the list. Create new reference for each iteration.

theList.Add(numbers);
    numbers = new int[9];
__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

You're right! I overlooked the == for != operator:$

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Andre -- upload your complete class here so we can look at it. What you posted had a slight compilation mistake or you were missing a member of the class when you posted code. That could be a make or break piece of information for solving your issue.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Scott I used this to track errors but I am still busy... Recursive (at least for me) is not really my cup of tea.

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        private static int[] numbers = new int[9]; 
        private static List<int[]> theList = new List<int[]>();

        static void Main(string[] args)
        {
            BackTrack(0);
            Print();
            Console.ReadKey();
        }

        private static void BackTrack(int p)
        {
            int i;
            for (i = 1; i <= 9; i++)
            {
                numbers[p] = i;
                if (CheckCorrectness(p))
                {
                    if (p == 8) AddSolution();
                    else BackTrack(p + 1);
                }
            }
        }

        private static bool CheckCorrectness(int p)
        {
            int i;
            for (i = 0; i < p; i++)
                if (numbers[i] == numbers[p]) return false;
            return true;
        }

        private static void AddSolution()
        {
            theList.Add(numbers);
            //uncomment line to that it generates the right solution
            //for (int i = 0; i < 9; i++) Console.Out.Write(numbers[i] + " ");
        }
       
        public static void Print()
        {
            for (int i = 0; i < theList.Count; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Out.Write(theList[i][j].ToString() + " ");
                }
                Console.Out.WriteLine();
            }
            Console.Out.WriteLine();
        }

    }
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Solved it! It was the List object. adatapost was partialy wright: I was adding the same instance of that object (but why where there only 9s?), but using new to generate new instances determins the loss of the numbers allready generated in the numbers object (it resets them with 0).

What did the trick was rewriting the AddSolution function, like this:

private void AddSolution()
 {
            theList.Add(new int[9]);
            for (int i = 0; i < 9; i++) theList[theList.Count - 1][i] = numbers[i];
}


Thank you all for your help.

andreivanea
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

1. @ the OP. Initialize the "int i" in the for loop
2. Why not declare the numbers 1-9 on your own?

Example:

int[] intArray = { 1,2,3,4,5,6,7,8,9 };
Poab9200
Light Poster
35 posts since Aug 2008
Reputation Points: 16
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You