none
How to define a dynamic jagged array using c#

    Question

  • Hello,

    I am trying to define a jagged array of strings. The first array needs to contain 7 strings, but each of those arrays needs to be dynamic. I can't find a good way to do it. I'm trying to use

    using System.Collections
    
    namespace CheckAndCorrectLayers
    {
      public class CurrentLayers 
      {
      }
      ArrayList AQSLLayers;
      AQSLLayers.Add("0");
      AQSLLayers.Add("clearance");
      AQSLLayers.Add("clearance-e");
      AQSLLayers.Add("hidden");
      AQSLLayers.Add("hidden-e");
      AQSLLayers.Add("detail");
      AQSLLayers.Add("doors-and-drawers");
    }

    This is my very first C#/.net program ever. Sorry if my code is completely messed up or I'm making noob mistakes. This just seems like too much trouble though. What I want to have is something like this

    public string[7][] AQSLLayers = {"0"[], "clearance"[],"clearance-e"[], "hidden"[], "hidden-e"[], "detail"[], "doors-and-drawers"[]}
    where the first array is initialized and the 7 other arrays are left dynamic.

    Tuesday, July 23, 2013 4:23 PM

Answers

All replies

  • If you make a jagged array, each subarray can be a different length. However, with an array, you need to know the length of the first rank when it's first initialized.

    If you need to have something that will grow as needed, then use a List<T> instead, ie:

    // Make a list of string arrays
    List<string[]> AQSLLayers = new List<string[]>();
    
    // Add in an array of 7 elements...
    AQSLLayers.Add(new[]{"0","clearance","clearance-e", "hidden", "hidden-e", "detail", "doors-and-drawers"});
    
    // Keep adding items (arrays of string) as needed
    AQSLLayers.Add(new[]{"foo", "bar", "baz"});
    
    


    Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Tuesday, July 23, 2013 4:29 PM
  • You could also do a list of lists (the list version of a jagged array):

    //List of string lists
    List<List<string>> ListOfStringLists = ....
    
    If you have any indication of the needed size in advance, use the Constructor wich takes a capacity first. While a list can grow automatically, such a step is costly (including temporarily more then doubling the needed memory).

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Tuesday, July 23, 2013 6:28 PM
  • Ultimately what I want to do is have 7 different lists. I want to take data from an external source (an AutoCAD drawing) and add it to the lists and save the contents of the list so that each time the program is run it has all of the items from the previous runs (store it in a text file or dll or somewhere that can be updated and referenced).

    List<T> seems like a good choice, because the 7 lists will need to constantly have items added, but it also seems like a good choice because my knowledge of C# is limited to general programming knowledge.

    Tuesday, July 23, 2013 6:59 PM
  • If you are really certian about the "7 Lists" part, this could do the trick:

    List<String>[] ArrayOfLists = ...;

    A List is an Array that has the ability to grow as needed when elements are added.

    The rest is actually a totall different Problem "How to persist data between runs of a Programm?" The usual approach is to use the UserSettings to store the data:

    http://msdn.microsoft.com/en-us/library/bb397750.aspx

    The data type used in the Settings can be an array as it is only a fixed representation. Just pick a String[][] for the settings.

    It is usually easy to transform between a List and a Array. List has a "toArray" function. And both a Constructor that takes an array, and a AddRange fucntion just for those cases.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Tuesday, July 23, 2013 7:11 PM
  • Thank you Christopher for the resources. I will be reading up on this. I am certain that there need to be 7 lists, I just want to put them in an array to access them more easily

    I have this typed.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CheckAndCorrectLayers
    {
        public class CurrentLayers
        {
            List<String>[] AQSLLayers = new List<String>[]{"0","clearance","clearance-e", "hidden", "hidden-e", "detail", "doors-and-drawers"};
        }
    }

    Visual Studio is telling me that I am trying to implicitly convert the strings to the type List<string>.

    Do I have to write a separate class to make the strings the correct type?

    Thank you for your help.

    Tuesday, July 23, 2013 8:41 PM
  • You can use a collection initializer to initialize the first list:

    List<String>[] AQSLLayers = new List<String>[]
    {
       // Initialize first list populated
       new List<string> {"0","clearance","clearance-e", "hidden", "hidden-e", "detail", "doors-and-drawers"},
       new List<string>(), // Initialize other 6 with empty lists
       new List<string>(),
       new List<string>(),
       new List<string>(),
       new List<string>(),
       new List<string>()
    };;
    


    Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Tuesday, July 23, 2013 9:17 PM