0

I am trying to instantiate a 2 dimensional array of strings. My issues is that the 2nd dimensions of the array are not all the same and I am not sure how to specify this code wise.

These variable specify electrical boards. There are 4 boards, each with a varying number of contacts. For each contact there is a description about its purpose.

int numBoards = 4;
String[] boardNames = {"FirstBoard", "Second Board", "Third Board", "Fourth Board"};
int[] numContacts = { 32, 24, 48, 32 };
String[][] descriptions = new String[numBoards][???];

How can I specify that my 2nd dimensions of the descriptions array are of varying sizes; the sizes specified in numContacts?

Is this the only way to do it? Or is there something more elegant?

int numBoards = 4;
String[] boardNames = {"FirstBoard", "Second Board", "Third Board", "Fourth Board"};
int[] numContacts = { 32, 24, 48, 32 };

String[] desc1 = new String[numContacts[0]];
String[] desc2 = new String[numContacts[1]];
String[] desc3 = new String[numContacts[2]];
String[] desc4 = new String[numContacts[3]];
String[][] descriptions = new String[numBoards][];
descriptions[0] = desc1;
descriptions[1] = desc2;
descriptions[2] = desc3;
descriptions[3] = desc4;
3
  • If that becomes larger you'd probably start putting the data into a file and then parse the file. Commented May 9, 2014 at 14:09
  • You used array initialized syntax for the inner arrays. Do the same for the containing array. Commented May 9, 2014 at 14:22
  • That said, this whole thing looks like a bad design. Make a class to represent boards. Commented May 9, 2014 at 14:24

3 Answers 3

2

You could use the object initialiser syntax to populate your array:

string[][] strings =
{
    new[] { "Fred", "Bob" },
    new[] { "Anne", "Steve", "John" }
};

If by elegant you mean syntax wise

Sign up to request clarification or add additional context in comments.

Comments

2

I think the more elegant solution would to not use a 2d array at all, but an array of Board classes.

public class Board {
  public Board(String name, int contactCount) {
    Name = name;
    Contacts = new List<String>(contactCount);
  }

  public String Name { get; set; }
  public List<String> Contacts { get; set; }
  ...
}

1 Comment

ninja'd me :P that is what I was going to suggest to since it is much easier on the eyes to read, and easier to manage too.
1

You can use a Linq query to generate the jagged array:

int[] numContacts = { 32, 24, 48, 32 };

String[][] descriptions = numContacts.Select(c => new string[c]).ToArray();

3 Comments

A pure 2-d would be something with the same values for each 2nd dimension? // So that will work to find both the first and second dimensions for the array?
@user1596244 I would say a 2-D array is a "rectangular" array, but it's a semantic issue and not relevant to your question, so I'll remove that part. The code I posted works to generate a jagged array.
Makes sense, I guess I just never heard it. // Thanks, worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.