Solved

Need to form the following array of array.

Posted on 2013-01-25
7
171 Views
Last Modified: 2013-01-27
Hi there;

sketch1
I try to form the following structure as array. But I am lost in dimensions.
Can someone aid me for C# of the attached image? (Java is also appreciated for learning purpose).

My code is  as follows which is obviously false..

  int[] flower = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[,] []arr = {{ flower, flower}};

Open in new window


This is for educational purpose.

Regards.
0
Comment
Question by:jazzIIIlove
  • 3
  • 2
  • 2
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38818936
Why a multi-dimensional array?...

You're implementing a tree with many elements and an unspecified depth.  I would recommend you actually use an object oriented approach with a List() contained within a Node Class.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38818980
I would suggest a Dictionary of Dictionary of Dictionary...  to mimic a tree.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 38819591
ok Dictionary seems an optimum solution.
So, suppose the depth is 3. What would be the Dictionary initialization for this?

I couldn't visualize this..Can you help me?

Regards.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 334 total points
ID: 38819723
The more I think about it, this could get messy as the number of levels increase. I think I was too focused on your graphic to think smarter at the time  : \

Nevertheless, here is the answer to your recent question:

var tree = new Dictionary<int, Dictionary<int, List<int>>>();

for (int level1 = 1; level1 < 10; level1++)
{
    tree.Add(level1, new Dictionary<int, List<int>>());

    for (int level2 = 1; level2 < 10; level2++)
    {
        tree[level1].Add(level2, new List<int>());
    }
}

tree[1][1].Add(4);

Open in new window


You might want to instead search for a tree implementation that is specific to your scenario. There are many different kinds of trees, so it would be difficult to suggest an implementation without knowing what was to be accomplished.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 38820595
Actually, I am trying to form a some kind of  tree idea in a way that I can do a search.
The image above is for a pattern search.
Set of values will be set in this tree such as:
35671 (numbers inc. level 5),  
3567 (numbers; 3 (level 1), 5(level 2), and 6 (level 3), 7 (level 4)) etc.

Then the input value is searched against the above values,
e.g. 356743
which will fail in 35671 but a success in 3567 since,
3567 in 356743 input matches 3567 in above list whereas,  that input doesn't match 35671 above since 1 kills it.

I think such an algorithm formation above and searching in those per level is a good solution. What do you think?

I thought it would be easier in an array formation that I can search upon*. Btw, your code works as i debug in VS but since you created dictionariy and lists on the fly, I cannot reach the lists directly. Hmm...

Thanks.

Regards.

*since the array of arrays seemed aligned to me and easy to retrieve corresponding value.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 334 total points
ID: 38820808
but since you created dictionariy and lists on the fly, I cannot reach the lists directly.
You can. You just have to index the appropriate levels.

e.g.

List<int> theList = tree[1][1];

...would correspond to:

Screenshot
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 166 total points
ID: 38820876
Gotcha...so you're implementing a sort of DAWG structure but with numbers?
http://www.wutka.com/dawg.html
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Where did System.Data.Objects go? 2 27
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
VB.NET 2008 Winforms Signing 13 19
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question