| | |
Use of List<int[]>
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
Hello. I am writing a program that uses a List<T> object to retain a list of int[] objects.
It's basicaly a backtracking algorithm that constructs the numbers object, which contains a permutatio of the numbers 1..9, and after every complete construction adds the solution to the list:
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):
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 with BackTrack(0) and the it calls the Print() method.
C# Syntax (Toggle Plain Text)
private int[] numbers = new int[9]; private List<int[]> theList = new List<int[]>();
It's basicaly a backtracking algorithm that constructs the numbers object, which contains a permutatio of the numbers 1..9, and after every complete construction adds the solution to the list:
C# Syntax (Toggle Plain Text)
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):
C# Syntax (Toggle Plain Text)
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 with BackTrack(0) and the it calls the Print() method.
Last edited by andreivanea; Jul 3rd, 2009 at 10:20 am. Reason: Forgot something ...
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
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!
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<int[]> object.
Your code adding same reference of an array into the list. Create new reference for each iteration.
C# Syntax (Toggle Plain Text)
theList.Add(numbers); numbers = new int[9];
Failure is not fatal, but failure to change might be. - John Wooden
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.
Scott I used this to track errors but I am still busy... Recursive (at least for me) is not really my cup of tea.
c# Syntax (Toggle Plain Text)
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(); } } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
Solved it! It was the List<T> 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:
Thank you all for your help.
What did the trick was rewriting the AddSolution function, like this:
C# Syntax (Toggle Plain Text)
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.
Last edited by andreivanea; Jul 3rd, 2009 at 5:30 pm.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Quicksorting linked list - simple algorithm (C)
- Merge 2 sorted list using ADT (C++)
- BIG problem with STL List Container (C)
- help with this code of link list (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
- Help w/ dynamic list funtction definitions (C++)
- Using the STL LIst Container, how do I create, write,read, and store in file. (C++)
- printing a linked list (C++)
Other Threads in the C# Forum
- Previous Thread: Check Whether Button Clicked or not - Please Help
- Next Thread: C# help pls....!!!
| Thread Tools | Search this Thread |
.net access algorithm angle array asp.net bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp database databasesearch datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net image input install interface java libraries list loop marshalbyrefobject math monodevelop mouseclick movingimage msword mysql operator path pause photoshop php picturebox pixelinversion platform post programming radians regex remoting richtextbox server sleep socket sql statistics string study system.servicemodel table tcpclientchannel text textbox thread time timer update usb usercontrol validation virtualization visualbasic visualstudio webbrowser winforms wpf wpfc# xml






